Results 1 to 4 of 4

Thread: Counter-intuitive behavior of QThreads signals/slots

  1. #1
    Join Date
    Sep 2008
    Location
    Delft, Netherlands
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Counter-intuitive behavior of QThreads signals/slots

    Hi all,

    I recently bumped into some curious behavior while implementing a program that uses QThreads.

    Consider a "MyThread" class (a subclass of QThread) that implements an "updateTimerFired()" slot method. MyThread's "run" method instantiates a QTimer, connect the timer's timeout() signal to MyThread's updateTimerFired() slot, starts the timer, then executes exec() -- starting MyThread's private exec() loop.

    The updateTimerFired() slot gets repeatedly executed as it should, but here's the catch: in what thread do you think that it runs?

    Much to my surprise, it is executed not in MyThread's thread, but in the thread that has instantiated MyThread (in my case, the main thread).

    To verify for yourself, you can download http://wopr.jigsaw.nl/wtf.tar.gz.

    I think this behavior is completely counter-intuitive. Upon examining the Qt documentation, I found only a single line that explains this behavior:

    http://doc.trolltech.com/4.4/threads...across-threads

    "With queued connections, the slot is invoked when control returns to the event loop of the thread to which the object belongs. The slot is executed in the thread where the receiver object lives."

    So what this boils down to is this: if a QThread derivative has a slot, it will not get executed by its own event loop, in its own thread. Therefore, you should be very wary of using slots in a QThread derivative, or even slots in a member function of a QThread-derived class. If you want a slot to be execute in the thread itself, this will only work if you instantiate the object with the slot within the run() method.

    My feeling (after spending a day of chasing this issue) is that the Qt documentation could (or rather should) be a lot more specific to this. In particular, I think this should be mentioned in the documentation of the QThread class.

    Any thoughts?

  2. #2
    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: Counter-intuitive behavior of QThreads signals/slots

    Quote Originally Posted by reddish View Post
    Upon examining the Qt documentation, I found only a single line that explains this behavior:[...]
    You will have to read that document once more. The paragraph that explains this behavior is:
    Like other objects, QThread objects live in the thread where the object was created -- not in the thread that is created when QThread::run() is called. It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex.
    Instead of subclassing QThread, put a QObject subclass inside the thread and all connections will behave as you expect.

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

    reddish (8th September 2008)

  4. #3
    Join Date
    Sep 2008
    Location
    Delft, Netherlands
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Counter-intuitive behavior of QThreads signals/slots

    Quote Originally Posted by jacek View Post
    You will have to read that document once more.
    Indeed I will, I missed that part completely. Thanks for pointing it out.

  5. #4
    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: Counter-intuitive behavior of QThreads signals/slots

    Be aware that you can push the QThread object to its own thread so that its slots will be executed in the context of the thread created by that object.

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.