PDA

View Full Version : thread synchronization



freekill
11th December 2009, 08:53
I'm trying to synchronize one producer thread and two consumer threads. All the threads are sharing an image buffer. The producer thread updates the image in the buffer every 33ms (webcam rate). Each consumer thread does individual and independent processing on the image.

My idea is that as soon as the producer thread updates the image in the buffer, it will wake up the consumer threads. The consumer threads will process the image as soon as woken up and when it is done with the process, the thread will be put to sleep.

What synchronization is most effective in implementing this? TIA.

wysota
11th December 2009, 08:56
Use a wait condition (QWaitCondition) or two semaphores.

nmkarvekar
11th December 2009, 09:59
You can use sem_wait and sem_post operations on semaphore

freekill
11th December 2009, 19:35
I have a few issues with wait conditions and semaphores. I would like to wait indefinitely in the consumer threads until I get a signal from the buffer that a new data is to be processed. When this happens, I wake up my consumer threads. However, I do not wish to wait for the consumer threads to finish up. If a new data is generated (by the producer thread) even before my consumer thread is done with its current process, I would like to put in a new data in the buffer and again alert and wake up sleeping threads (assuming other threads are done with its own process) to do another processing. If I am to use wait conditions here, I would need to finish up all the consumer threads' activity before my producer thread is allowed to write data on the buffer wouldn't it?

p.s. Do I need to use a mutex even just for reading data in a buffer? Multiple threads will have to read the same data simultaneously.

wysota
11th December 2009, 20:28
Take a look at QReadWriteLock. Just remember that without a semaphore you are vulnerable to buffer underruns.

freekill
12th December 2009, 01:37
I may have buffer overruns, actually I'm expecting that for one of my consumer threads since this thread will have to update image in my GUI (which I believe takes too much time). It may or may not keep up with my 33ms limit but its fine. I don't have to wait for it to finish anyway. More concern will come to the computing thread which will do the math on the image I have to make this thread finish everything before a new data is placed on the buffer.

...A little bit of hardware concern - will I not have any error if 2 threads are trying to access the same exact data in my buffer? The threads MAY simultaneously access the same memory address for the data.

Tanuki-no Torigava
12th December 2009, 01:56
Hi, you can quickly look into examples from the book "Foundations of Qt Development" here (http://www.apress.com/book/view/1590598318) in download sections. Look for examples at chapter 13.

Best regards,

-- Tanuki

P.S. Also I suggest you to look into QFuture. That might save you a couple of classes.

wysota
12th December 2009, 09:49
I may have buffer overruns, actually I'm expecting that for one of my consumer threads since this thread will have to update image in my GUI (which I believe takes too much time). It may or may not keep up with my 33ms limit but its fine.
You really want your application to crash? How strange...


...A little bit of hardware concern - will I not have any error if 2 threads are trying to access the same exact data in my buffer?

If both of them read the data then not.

freekill
13th December 2009, 03:14
You really want your application to crash?

The data overrun I'm expecting wouldn't really make my program crash. It's just that the objective of this consumer thread as I will call an imaging thread is just to update the image in the GUI. I do no other process here simply display. So when overrun happens, the only thing that happens is that the image doesn't get to be updated for that frame - nothing fatal. A more vital operation runs on my computing thread as this is where the operation of the whole program runs. I would definitely make this sync with my producer thread.

wysota
13th December 2009, 09:51
So don't you have some kind of queue or list you read the data from? Because if you do then reading from an empty queue will cause your application to crash. You need a semaphore to avoid that.

freekill
6th January 2010, 18:29
I settled for QWaitCondition with the idea of having my image buffer alert sleeping threads via waitCondition.wakeAll() and have the run() functions of the rendering and computing threads sleep when done with its current processing via waitCondition.wait(). I kept pointers to the waitCondition to allow the various threads access to it. However, I'm having this compile-time error in my computing thread: no matching function for call to 'QWaitCondition::wait()'.


//window.h
Private:
QWaitCondition waitCondtion;
QWaitCondition* pWaitCondition;
ComputeThread* computeThread;

//window.cpp
Window::Window()
{
pWaitCondition = waitCondition;
computeThread = new ComputeThread(pWaitCondition);
}

//computethread.h
Private:
QWaitCondition* newImage;

//computethread.cpp
ComputeThread::ComputeThread(QWaitCondition* wc)
{
newImage = wc;
}

void ComputeThread::run()
{
newImage->wait(); //error goes here
}