Results 1 to 13 of 13

Thread: Are signals and slots thread safe?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Are signals and slots thread safe?

    Is it thread safe to exchange data between threads using signals and slots, such that the signal is emitted in the sending thread and the slot is executed in the receiving thread, i.e. using queued connections?

    And if so, is this also true for passing implicitely shared containers?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    I have read that passage many times and it doesn't explicitely say that using queued connections is thread safe. The best clue is perhaps the Mandelbrot example, that uses queued connections to transfer a QImage and no mutex or anything is used to protect access. But still, before I believe it, I would like to hear someone saying it out loud.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    This passage says:
    Queued Connection: The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
    And if this is not explicit enough, then the previous chapter states:
    Threads and QObjects

    QThread inherits QObject. It emits signals to indicate that the thread started or finished executing, and provides a few slots as well.

    More interesting is that QObjects can be used in multiple threads, emit signals that invoke slots in other threads, and post events to objects that "live" in other threads. This is possible because each thread is allowed to have its own event loop.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    I don't mean to be a pest, but no. For me this is not explicit enough. It says that connections can be made between threads and that slots are executed in the receiver's thread, but how and why is that thread safe? If I wasn't using signals and slots and just call a setter method to copy data from one thread to another, and some method uses the data in the receiver thread when control returns to it it sounds like exactly the same procedure, but it's not thread safe.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    It says that connections can be made between threads and that slots are executed in the receiver's thread, but how and why is that thread safe?
    Because the execution is kept in the respecting threads.
    It means that when you emit a signal from thread A, the slot it is connected to runs in thread B - which is not the same as thread A running the slot which lives in thread B, but thread B will run its own slot, triggered by the signal (event) it received form thread A.
    So what is NOT thread safe about it?

    Queued connection means basically, that you signals are translated to events, which are posted to the target thread.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Are signals and slots thread safe?

    Quote Originally Posted by Cruz View Post
    For me this is not explicit enough.
    Let's make it explicit then. Just please explicitly state whether you are asking about making signal-slots connections or what? Emitting signals? Executing slots? What exactly? Concepts are not thread-safe, code can be thread-safe so let's talk about code and not concepts.
    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.


  8. #8
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    only the call to a slot is thread safe, if you are using some shared data between your threads ( and using it in that particular slot ), then you still have to protect it.

  9. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Are signals and slots thread safe?

    Quote Originally Posted by Cruz View Post
    Is it thread safe to exchange data between threads using signals and slots, ...
    And if so, is this also true for passing implicitly shared containers?
    If you are passing in connection Qt containers by const reference or a value (shallow copy is made) it is thread safe. If one of threads will make some changes in container it will automatically create a deep copy in thread safe manner, that is way Qt documentation recommends to avoid use of operator[] (it is not const so it will create deep copy if needed) if you only read values.
    If you are passing those by reference or pointer then you have to maintain thread safety.

  10. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    Ok, here is the code as requested:
    Qt Code:
    1. class Thread : QThread
    2. {
    3. TransportObject transportObject;
    4.  
    5. public slots:
    6. objectIn(TransportObject);
    7. }
    8.  
    9. Thread::Thread(QObject *parent) : QThread(parent)
    10. {
    11. moveTothread(this); // Thread handles it's own event loop hack.
    12. start();
    13. }
    14.  
    15. Thread::run()
    16. {
    17. forever
    18. {
    19. process(transportObject); // do something with transportObject
    20. QApplication::processEvents(); // handle pending events
    21. msleep(10);
    22. }
    23. }
    24.  
    25. Thread::objectIn(TransportObject to)
    26. {
    27. this->transportObject = to;
    28. }
    29.  
    30. main
    31. {
    32. Thread thread;
    33. TransportObject transportObject;
    34. connect(this, SIGNAL(objectOut(TransportObject)), &thread, SLOT(objectIn(TransportObject)));
    35. ...
    36. emit objectOut(transportObject);
    37. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Are signals and slots thread safe?

    if TransportObject is QList or QVector or other Qt container then it is fine.

    edit: But moving thread to it slef is a bad thing (lets asume that you will connect to deleteLater for thread then you are in big trouble).

  12. #12
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Are signals and slots thread safe?

    Quote Originally Posted by MarekR22 View Post
    if TransportObject is QList or QVector or other Qt container then it is fine.
    TransportObject has a custom struct, a QHash and a QList. Is it still fine then?



    Quote Originally Posted by MarekR22 View Post
    But moving thread to it slef is a bad thing (lets asume that you will connect to deleteLater for thread then you are in big trouble).
    Can you explain what happens if I connect to deleteLater()? So far I haven't encoutered any problems with moving a thread to itself. And the one line of code needed to do it is very nice compared to this construction.

  13. #13
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Are signals and slots thread safe?

    If this structure doesn't contain any pointers then it should be Ok. Qt will crete copy from those data so you don't have to synchronize anything.

    About moving thread to it self: deleteLater will try to delete QThread and when it is moved to it self deletion will be performed inside this thread, so it can't be finished properly since it will try to destroy event loop which is currently processed. Similar problem you will have when using start slot of QThread (use of not existing event loop).

  14. The following user says thank you to MarekR22 for this useful post:

    Cruz (21st April 2011)

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2011, 07:55
  2. SQL module and Thread-safe
    By banita in forum Qt Programming
    Replies: 6
    Last Post: 12th August 2010, 05:30
  3. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  4. Why is QMutex Thread Safe?
    By Kind Lad in forum Newbie
    Replies: 3
    Last Post: 22nd February 2010, 04:46
  5. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38

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.