Results 1 to 11 of 11

Thread: thread synchronization

  1. #1
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default thread synchronization

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: thread synchronization

    Use a wait condition (QWaitCondition) or two semaphores.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2006
    Posts
    10
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: thread synchronization

    You can use sem_wait and sem_post operations on semaphore

  4. #4
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: thread synchronization

    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.
    Last edited by freekill; 11th December 2009 at 19:39. Reason: added p.s.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: thread synchronization

    Take a look at QReadWriteLock. Just remember that without a semaphore you are vulnerable to buffer underruns.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: thread synchronization

    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.

  7. #7
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: thread synchronization

    Hi, you can quickly look into examples from the book "Foundations of Qt Development" here 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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: thread synchronization

    Quote Originally Posted by freekill View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: thread synchronization

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: thread synchronization

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: thread synchronization

    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()'.
    Qt Code:
    1. //window.h
    2. Private:
    3. QWaitCondition waitCondtion;
    4. QWaitCondition* pWaitCondition;
    5. ComputeThread* computeThread;
    6.  
    7. //window.cpp
    8. Window::Window()
    9. {
    10. pWaitCondition = waitCondition;
    11. computeThread = new ComputeThread(pWaitCondition);
    12. }
    13.  
    14. //computethread.h
    15. Private:
    16. QWaitCondition* newImage;
    17.  
    18. //computethread.cpp
    19. ComputeThread::ComputeThread(QWaitCondition* wc)
    20. {
    21. newImage = wc;
    22. }
    23.  
    24. void ComputeThread::run()
    25. {
    26. newImage->wait(); //error goes here
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Thread Synchronization Problem
    By sijithjames in forum Qt Programming
    Replies: 13
    Last Post: 13th November 2009, 15:53
  2. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 01:18
  3. Replies: 3
    Last Post: 30th June 2008, 16:23
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.