Yard072999
asked on
Queing into a array
Hi, I want put data (string) to an array for query sql,
like
string1 ="insert into table ..."
string2 ="insert into table1 ..."
string3 ="insert into table2 ..."
string4 ="insert into table3 ..."
I want put them into an array for "execute" them later with a pthread process, I have the code for start a thread, but I can't figure how I can fill an array with that.... my thread will check if the array is empty, is yes it will loop, else it will send the query to my database. In some time, my main process can add stuff to my array... you see the point?
how can I do that? I'm still learning c.
like
string1 ="insert into table ..."
string2 ="insert into table1 ..."
string3 ="insert into table2 ..."
string4 ="insert into table3 ..."
I want put them into an array for "execute" them later with a pthread process, I have the code for start a thread, but I can't figure how I can fill an array with that.... my thread will check if the array is empty, is yes it will loop, else it will send the query to my database. In some time, my main process can add stuff to my array... you see the point?
how can I do that? I'm still learning c.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Yard,
One HUGE item that you're going to have to manage is data locking. It's not hard to do this, but it is critical.
For instance, imagine trying to insert the string "INSERT into table MYTABLE (VAR1, VAR2, VAR2) values (\"X1\", \"X2\", \"X3\");" into your C table from one thread. However, the string copy process (likely strcpy() or sprintf()) is interrupted so that the O/S can assign the CPU to the other thread. The thread might only recieve "INSERT into table MYT" and try to execute it. The result will be an SQL error.
You can manage this several ways. The main thread can use the Suspend() and Resume() methods to control when the second thread is executing. But you'll also want to know when the second thread is finished so that you know when it's safe to modify your table. Use a variable to control which thread is locking the table.
int TableLock = 0;
/* Main thread */
if (TableLock == 0) /* Safe to change the table */
{
/* Make table Changes */
TableLock = 1; /* Tell the second thread to run */
}
/* Second thread */
if (TableLock) /* The maint thread is not locking the table */
{
/* Run the statements */
TableLock = 0; /* Tell the main thread that this thread is done */
}
These simple controls will manage the two threads in a way that keeps them from stepping on each other.
Kent
One HUGE item that you're going to have to manage is data locking. It's not hard to do this, but it is critical.
For instance, imagine trying to insert the string "INSERT into table MYTABLE (VAR1, VAR2, VAR2) values (\"X1\", \"X2\", \"X3\");" into your C table from one thread. However, the string copy process (likely strcpy() or sprintf()) is interrupted so that the O/S can assign the CPU to the other thread. The thread might only recieve "INSERT into table MYT" and try to execute it. The result will be an SQL error.
You can manage this several ways. The main thread can use the Suspend() and Resume() methods to control when the second thread is executing. But you'll also want to know when the second thread is finished so that you know when it's safe to modify your table. Use a variable to control which thread is locking the table.
int TableLock = 0;
/* Main thread */
if (TableLock == 0) /* Safe to change the table */
{
/* Make table Changes */
TableLock = 1; /* Tell the second thread to run */
}
/* Second thread */
if (TableLock) /* The maint thread is not locking the table */
{
/* Run the statements */
TableLock = 0; /* Tell the main thread that this thread is done */
}
These simple controls will manage the two threads in a way that keeps them from stepping on each other.
Kent
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>, I have the code for start a thread, but I can't figure how I can fill an array with that....
Where do you get this information from? Is it a file or user inputs them interactively or is there some other source?
Reading from a source and filling it into an array is not quite difficult.
fgets (buffer, BUF_LEN, fp );
will read a single *line* from fp file pointer up to maximum BUF_LEN-1 bytes. Replace fp with stdin to get the query from the user. However, this gets tricky when length of query exceeds BUF_LEN as you will need more memory to store the entire query.
An array of char * should serve the purpose. Can you post some more details along with the code snippets that you have already written.
cheers
sunnycoder