Queing into a array

Posted on 2004-09-23
Last Modified: 2010-04-15
Hi, I want put data (string) to an array for query sql,


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.
Question by:Yard072999
LVL 55

Accepted Solution

Jaime Olivares earned 250 total points
ID: 12138996
Easiest method (not the best necessarily) is to declare as a fixed array, maybe global:

#define MAX_QUERIES 20
#define QUERY_LEN 200

since array has a fixed number of element, you will need a size control variable, let's say:

int LAST_QUERY = 0;

now, every time you want to add a query to list do something like that:

strcpy(MYQUERIES[LAST_QUERY++], "insert into table....");

finally to loop all your queries somewhere:

int i;
for (i=0; i<LAST_QUERY; i++) {

Hope to be useful,
LVL 45

Expert Comment

ID: 12140730
Hi Yard,

>, 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.

LVL 45

Expert Comment

ID: 12143185
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.


Assisted Solution

CmdrRickHunter earned 250 total points
ID: 12153677
that will keep two threads executing in a perfect lockstep.
Two problems with it:
1) your code will busy wait.  This means that while the second thread is waiting for input from the first, it sits, using up all of its time checking if the 1st is done.  You instantly just lost 50% of your CPU power.
2) If all you have to do is prepare a table, then run the second thread... is there such a gain of having two threads?  If you want to be able to write to the "queue" while you're executing a previous query, you'll have to wait for the queue to empty.

pthreads offer a few things you might find useful: mutexes and events.

mutexes are a construct that a thread "owns".  You aquire a mutex, and no other thread can aquire it until you release it.  Think of it like a locking mechanism.  When you want to read from/write to the table, you aquire the mutex.  This means that, as long as you hold the mutex, no one else can be working with the table (unless they're being bad... if they completly ignore the mutex, it wont do you any good).  once you have the mutex, you can write to the array without fear of someone else reading it at a bad time.  You then release the mutex, and go on your merry way.  The reader locks it, reads, then unlocks it.

events help the "busy wait" syndrome - where you keep checking over and over if there's new data available. The thread that does the queries waits for the "add item" event to occur.  Only when that event occurs does it try to read  a string (using the mutexes to lock the queue).  The thread that adds queries first adds the query (using the mutexes to lock the queue), then signals the event.  Unlike polling to see if there's new items, "waiting" on an event (or a mutex, or any of the syncronization objects) is almost free... takes almost zero CPU time).

An alternative to a shared memory queue would be a pipe.  Unix programs use these quite often to pass information between processes.  They're slower than shared memory, but they are really easy to work with... and the "slowness" of pipes will be minimal compared to the time it takes to make a SQL query (probably minimal compared with the time it takes to even PARSE the SQL query string, much less execute it):

int thePipe[2]; // thePipe[0] is for input, thePipe[1] is for output
if (pipe(thePipe) != 0) {
 // check errno, etc
spawn threads.

fprintf(thePipe[1], "MyString");

fscanf(thePipe[0], "%s", buffer)

thePipe is a pair of file-descriptors, so anything you can do on a file descriptor (wait, read, write, select, etc) you can do on thePipe.

My advice is to search for pipe IPC (Inter Process Communication).  There are people who are much better with examples/tutorials than I.

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with MFCApp 78 383
C simple state machine for client server handshake 4 300
C Language combined operators 28 109
How to install SVN Command Line Client? 5 66
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question