views:

372

answers:

2

Hi, I am using a application where lower level application always invokes a callback RecData(char *buf) when it receives the data. Now in the call back i am creating two threads and pass the consumer and producer function to these created threads respectively. My code looks like as below:

void RecData (char * buf) {

CreateThread(NULL,0,producer_queue,(void *)buf,0,NULL);
CreateThread(NULL,0,consumer_queue,NULL,0,NULL);

}

The above works fine when i receive only one data at a time. But if i receive say 5 data almost at the same time then producer_queue should first put all the data in queue and then consumer_queue should start retrieving the data but here as soon as producer_queue put 1st data in queue, consumer_queue retrieves it.

Please help me in implementing the above in correct way. I am new to the thread/queue implementation. Thanks

+1  A: 

What you want to do, I believe, is control access to the queue. You'll want to look at using a mutex to control reading from the queue.

When you recieve data, you will lock the mutex, then enqueue data. When you are done queing the data, then release the lock.

When reading from the queue, you will see if the mutex is locked. If you are writing data to the queue, you won't be able to start reading, until your producer thread has completed writing all of it's data and release the lock. If you actually lock the mutex, then you prevent your writer thread from writing while you are reading data.

This approach could introduce potential deadlocks. If your writer thread dies prior to releasing the lock, then your reader thread will not be able to continue (then again your thread dying may just trigger an error state).

I hope this makes sense.

Alan
Hi Alan, What happens actually with the above code which i mentioned in my Ques is that thread for the producer and the thread for the consumer are always created one after each other.
Say if the RecData (char * buf) callback receives 5 data almost at a same time then for writing 1st data in queue it creates a thread for the producer which writes it into the queue and then next it creates one more thread(consumer) to read the data and the same happens for the next data also.
I want it to behave like when it receives 5 data the producer should write all the 5 data in queue and then consumer thread should start reading these 5 data from the queue. Is it possible?Thanks
Are you saying that RedData is being called 5 seperate times?
Alan
yes..exactly..each time a write a data in the queue then next consumer_queue is called to read the data..
Do you know ahead of time how many data items to expect? Also, why do you want to queue up all 5 items prior to reading them? Why can you queue and dequeue independantly?
Alan
No i dont know how many data items i m going to expect..also i want to make queue and dequeue independent..but how to do that..Say if i just put only queue thread then it will queue all the data..but then from where i'll invoke dequeu thread..?
You can spawn the dequeue at anytime. The dequeue thread could loop, checking the queue for data.
Alan
A: 

Use the concept of condition variables. The probelm you have is the most common one in multi-threaded programming world. Just using mutexes doesn't help the situation. Always remember that mutexes are for locking & condition variables are for waiting. The later is always safer and almost certain when a thread should start consuming from a shared queue.

Check out the below link on how you can create a condition variable on your own on windows: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html

If you are using windows vista, the below msdn example may help you: http://msdn.microsoft.com/en-us/library/ms686903(VS.85).aspx

In all cases use the logic as shown in Schmidt's website as it looks more portable (oh yes portable on different versions of windows atleast). Schmidt's implemention gives you the standard POSIX api feel which is the widely used standard on most modern UNIX/LINUX systems.

placidhacker
Your msdn link is a little borked.
Simon Buchan