Results 1 to 4 of 4

Thread: Multi-threading behavior of signals and slots

  1. #1
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Multi-threading behavior of signals and slots

    Suppose I have an object A living in thread a and object C living in thread c. Object A has a signal sig1 which is connected to a particular slot of object C (default connection, which means in this case queued connection since it is cross-thread).
    Suppose thread a is running and object A emits signal sig1 twice before thread c runs. After that, thread c runs. Will the slot of object C be called once or twice?

    Similarly to the situation above, add a thread b with object B which has a signal sig2 that is connected to the same slot of object C. Situation: thread a runs, object A emits signal sig1. Then thread b runs, object B emits signal sig2. Then thread c runs. Will the slot of object C be called once or twice?

    Thank you for any insights.

  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: Multi-threading behavior of signals and slots

    Quote Originally Posted by T_h_e_o View Post
    Suppose thread a is running and object A emits signal sig1 twice before thread c runs. After that, thread c runs. Will the slot of object C be called once or twice?
    Twice.

    Similarly to the situation above, add a thread b with object B which has a signal sig2 that is connected to the same slot of object C. Situation: thread a runs, object A emits signal sig1. Then thread b runs, object B emits signal sig2. Then thread c runs. Will the slot of object C be called once or twice?
    Twice.

    I know I posted a reply to your thread pretty quickly, but wouldn't it give you more satisfaction to just write an example program and test it yourself?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QThread>
    3. #include <QtDebug>
    4.  
    5. class A : public QObject {
    6. Q_OBJECT
    7. public:
    8. A(){}
    9. public slots:
    10. void emitSignal() { emit sig1(); }
    11. signals:
    12. void sig1();
    13. };
    14.  
    15. class C : public QObject {
    16. Q_OBJECT
    17. public:
    18. C(){}
    19. public slots:
    20. void someSlot() { qDebug() << Q_FUNC_INFO; }
    21. };
    22.  
    23.  
    24. #include "main.moc"
    25.  
    26. int main(int argc, char **argv) {
    27. QCoreApplication app(argc, argv);
    28. A *a = new A;
    29. C *c = new C;
    30. QObject::connect(a, SIGNAL(sig1()), c, SLOT(someSlot()));
    31.  
    32. c->moveToThread(&t);
    33.  
    34. a->emitSignal();
    35. a->emitSignal();
    36. t.start();
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    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. The following user says thank you to wysota for this useful post:

    T_h_e_o (11th January 2013)

  4. #3
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multi-threading behavior of signals and slots

    Wysota, thanks, that was really fast!
    Considering your reputation I trust your answer is 100% correct, but viewing your example: how do you guarantee that the two signals of a are *both* emitted before the main thread is executed?

    As for your question: I'm still in the process of thinking about different solutions, so no software available yet to easily put some test code into. Wouldn't have expected that a piece of code to test it could be *that* simple... Guess that's a matter of experience (hey, I didn't post this in the newbie forum for nothing...).

    Anyway, thanks again.

  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: Multi-threading behavior of signals and slots

    Quote Originally Posted by T_h_e_o View Post
    how do you guarantee that the two signals of a are *both* emitted before the main thread is executed?
    Signals are always emitted immediately. It is slots that can be executed later depending on thread affinity.

    As for your question: I'm still in the process of thinking about different solutions, so no software available yet to easily put some test code into.
    You can see a complete test case in my previous post, there is no need for any special software apart Qt itself.
    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.


Similar Threads

  1. Multi-threading
    By plopes21 in forum Qt Programming
    Replies: 6
    Last Post: 2nd January 2013, 18:45
  2. Counter-intuitive behavior of QThreads signals/slots
    By reddish in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 21:40
  3. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 03:47
  4. Multi threading ...
    By kiranraj in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2007, 17:51
  5. Odd behavior Signals and Slots
    By Arthur in forum Qt Programming
    Replies: 6
    Last Post: 17th April 2006, 23:08

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.