Results 1 to 1 of 1

Thread: understanding problem of threads, mutexes and waitconditions

  1. #1
    Join Date
    Jan 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default understanding problem of threads, mutexes and waitconditions

    I seem to have a major understanding problem with threads, mutexes and waitconditions. Assume I have the following thread class:
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. MyThread() : QThread(), exec( true )
    5. {
    6. start();
    7. }
    8.  
    9. void run()
    10. {
    11. mutex.lock();
    12.  
    13. while( exec )
    14. {
    15. // do stuff here
    16. // ...
    17.  
    18. // wait for wake up
    19. condition.wait( &mutex );
    20. }
    21.  
    22. mutex.unlock();
    23. }
    24.  
    25. void stop()
    26. {
    27. QMutexLocker m( &mutex );
    28. exec = false;
    29. condition.wakeAll();
    30. wait();
    31. }
    32.  
    33. private:
    34. bool exec;
    35. QMutex mutex;
    36. QWaitCondition condition;
    37. };
    To copy to clipboard, switch view to plain text mode 
    Within the thread, I lock its QMutex outside the loop right away. Within the loop, stuff gets done, then the thread gets put to sleep by the wait condition and the QMutex gets unlocked, as far as I understand how QWaitCondition works.

    When the thread is supposed to be stopped by the main thread via the stop() function, a lock on the QMutex is requested again, to ensure the main thread waits until the thread enters the wait condition again, then the loop variable is changed to false to make the thread exit after the next loop, the main thread wakes up the QThread and waits for it to exit.

    Somewhere in this chain of logic I am missing something, because this way, a deadlock is created. When the main thread calls stop(), it waits forever for the thread to finish while the thread waits forever to be woken up.


    // edit: ugh, nvm, ... one day of frustration and now at lunch while being away from the code I notice the simplest thing of course: I should unlock the mutex before waiting for the thread to finish :P
    Qt Code:
    1. void stop()
    2. {
    3. mutex.lock();
    4. exec = false;
    5. condition.wakeAll();
    6. mutex.unlock();
    7. wait();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Spooky; 28th April 2011 at 13:20.

Similar Threads

  1. Replies: 3
    Last Post: 5th January 2011, 08:54
  2. Replies: 3
    Last Post: 18th July 2010, 14:53
  3. Replies: 4
    Last Post: 7th March 2010, 17:58
  4. Problem with threads
    By kvnlinux in forum Newbie
    Replies: 7
    Last Post: 21st August 2009, 18:29
  5. Threads problem
    By boss_bhat in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2006, 16:41

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
  •  
Qt is a trademark of The Qt Company.