Results 1 to 4 of 4

Thread: Thread synchronization, QWaitCondition, and recursiveMutex

  1. #1
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread synchronization, QWaitCondition, and recursiveMutex

    Hi all,

    just need a hint from somebody.

    I have several classes derived from QObject acting as some kind of "managers" with signals and slots. Each of this objects runs in its own thread. This way, it's easy to serialize signals by using queued signal/slots connections. Inside the objects, there is a QList which has to be protected by a mutex.

    Something like this:
    Qt Code:
    1. class ManagerThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. ManagerThread( QObject * parent = 0 ) : QThread( parent ) {}
    6. protected:
    7. void run() { exec(); }
    8. };
    9. ...
    10. class XYManager : public QObject
    11. {
    12. Q_OBJECT
    13. QList<int> list;
    14. mutable QMutex* mutex;
    15. public:
    16. XYManager(void);
    17. QList<int> GetList() const {
    18. QMutexLocker locker(mutex);
    19. return listNamesProgInfo;
    20. };
    21. public Q_SLOTS:
    22. void initialize();
    23. void addToList(int a);
    24. Q_SIGNALS:
    25. void signal_ListChanged();
    26. };
    27.  
    28. ...
    29.  
    30. xy_manager = new XYManager(*this);
    31. threadXY = new ManagerThread();
    32. threadXY->start();
    33. xy_manager->moveToThread(threadXY);
    To copy to clipboard, switch view to plain text mode 

    Now I can e.g. invoke a slot either with QMetaObject::invokeMethod or with a signal/slot connection in xy_manager which performs some action with a queued connection.

    So far, so good. But: if I want the thread which invokes the method in the manager to wait for completion of the method, how can I achieve that?

    Something like
    Qt Code:
    1. QMetaObject::invokeMethod(xy_manager, "initialize", Qt::BlockingQueuedConnection);
    2. // wait here until initialize has finished.
    To copy to clipboard, switch view to plain text mode 

    As far as I know, a BlockingQueuedConnection waits until the signal is delivered but not until the method has finished.
    So, an idea might be to use a QWaitCondition. But: how do I wait for the condition? Does the manager has to make the QWaitCondition available then via a get-method? And further: Since several methods inside the manager access the list, I use a recursive mutex to avoid a deadlock inside the manager. As far as I understand, recursive mutexes don't work with QWaitConditions, right?

    I am a bit puzzled on this one. Any help?

    Thanks,

    Rainer

  2. #2
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread synchronization, QWaitCondition, and recursiveMutex

    Quote Originally Posted by RThaden View Post
    Hi all,

    Something like
    Qt Code:
    1. QMetaObject::invokeMethod(xy_manager, "initialize", Qt::BlockingQueuedConnection);
    2. // wait here until initialize has finished.
    To copy to clipboard, switch view to plain text mode 
    Rainer
    Not sure about that: does the invokeMethod above with the BlockingQueuedConnection return when the signal has been delivered or when the desired slot has been fully executed? Last option would solve my problem.

    Kind regards,

    Rainer

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread synchronization, QWaitCondition, and recursiveMutex

    Quote Originally Posted by RThaden View Post
    Not sure about that: does the invokeMethod above with the BlockingQueuedConnection return when the signal has been delivered or when the desired slot has been fully executed?
    The blocking is done with a semaphore which is released in QMetaCallEvent destructor, so the sender is blocked as long as the meta call event object exists. AFAIK when event loop picks up the meta call event it first executes the slot and then deletes the event.

  4. The following user says thank you to jacek for this useful post:

    RThaden (28th August 2008)

  5. #4
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Thread synchronization, QWaitCondition, and recursiveMutex

    Thanks jacek,

    I think I'll write a small program to test the behaviour.

    Kind regards,

    Rainer

Tags for this Thread

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.