Results 1 to 12 of 12

Thread: Thread related questions

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Thread related questions

    Hi There!

    I just wanted to ask about some details that are not clear to me after reading the documentation.

    I intend to exchange data between threads using signals and slots. In the receiving thread, do I have to call exec() for the slots to fire? And when is the best time to do so? In the constructor? At the begining of run()? Or with each iteration of my infinite loop in the run() method?

    When using queued connection and there are multiple signals waiting n the queue for the same slot, are they all guaranteed to be executed or only the last one in the queue?

    Thanks
    Cruz

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

    Default Re: Thread related questions

    do I have to call exec()
    Not for the signals and slots.
    exec() is for running a local thread event loop.

    And when is the best time to do so?
    When the data is ready to be sent.
    But in truth, there is no absolute answer to this question - it depends on the inner logic of your application.
    If you supply more information what the thread is doing, and what data it wants to send and for what purpose, it will be easier to say.

    When using queued connection and there are multiple signals waiting n the queue for the same slot, are they all guaranteed to be executed or only the last one in the queue?
    All of them will be executed, if you do not do something in your code to prevent it.
    ==========================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. The following user says thank you to high_flyer for this useful post:

    Cruz (21st February 2011)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread related questions

    Oh and most importantly: using signals and slots is thread safe, right?

  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: Thread related questions

    Quote Originally Posted by high_flyer View Post
    Not for the signals and slots.
    exec() is for running a local thread event loop.
    Well, the thing is slots are delivered across threads using events so you need an event loop running to receive a cross-thread signal. You don't need it for emitting signals though.
    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.


  6. The following user says thank you to wysota for this useful post:

    bob2oneil (4th April 2011)

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

    Default Re: Thread related questions

    Well, the thing is slots are delivered across threads using events so you need an event loop running to receive a cross-thread signal.
    You need the application event loop, not a local thread one.
    You will receive signals, in a thread without a local event loop running, and you can send signals from such a thread object as well.
    ==========================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.

  8. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Thread related questions

    I think you need two event loops to have correct synchronisation.

  9. #7
    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: Thread related questions

    Quote Originally Posted by high_flyer View Post
    You need the application event loop, not a local thread one.
    No, you need a local event loop.

    Check out this example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Object : public QObject {
    4. Q_OBJECT
    5. public slots:
    6. void someSlot() { qDebug() << Q_FUNC_INFO; }
    7. };
    8.  
    9. #include "main.moc"
    10.  
    11. class Thread : public QThread {
    12. public:
    13. Thread() { timer.start(1000); }
    14. void run() {
    15. Object o;
    16. connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
    17. forever {
    18. ; // do nothing
    19. }
    20. }
    21. private:
    22. QTimer timer;
    23. };
    24.  
    25. int main(int argc, char **argv){
    26. QApplication app(argc, argv);
    27. #if 1
    28. QTimer timer;
    29. Object o;
    30. QObject::connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
    31. QThread thread;
    32. thread.start();
    33. o.moveToThread(&thread);
    34. timer.start(1000);
    35. #else
    36. Thread thread;
    37. thread.start();
    38. #endif
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    By default there is a local event loop running, if you change "#if 1" to "#if 0" there will be no local event loop running. See the result in both cases.
    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.


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

    Default Re: Thread related questions

    if you change "#if 1" to "#if 0" there will be no local event loop running.
    how so?
    Why don't you have an event loop in #else?
    Last edited by high_flyer; 22nd February 2011 at 10:34.
    ==========================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.

  11. #9
    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: Thread related questions

    Because in Thread::run() there is no call to QThread::exec().
    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.


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

    Default Re: Thread related questions

    By default there is a local event loop running,
    Because in Thread::run() there is no call to QThread::exec().
    Ah, yes, of course (docs):
    By default, run() starts the event loop by calling exec()
    Hmm.. it just occurred to me, that I as good as never used slots in my QThreads, rather I use set functions - which is where my confusion came form.
    So far I didn't use slots, since if I was setting something in my thread, I wanted it at that time, and not leaving it to the event loop to decide.
    ==========================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.

  13. #11
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread related questions

    In Wysota's example all signal processing is taking place in the thread. In #if 1 The object is moved to the thread, in #else there is just the thread sending signals to itself. So in this case Wysota is right, you need the event loop.

    What high_flyer said, however, is that if you send signals between the main thread and a worker thread, you don't need to start event processing in the worker thread, because the main thread handles it. Having tried it this way I can confirm that it works. My thread receives signals from main and signals from the thread to main are also received and I never called exec().

  14. #12
    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: Thread related questions

    Quote Originally Posted by Cruz View Post
    in #else there is just the thread sending signals to itself.
    No, that's not true. The timer lives in the main thread and the "Object" lives in the worker thread.

    Having tried it this way I can confirm that it works. My thread receives signals from main and signals from the thread to main are also received and I never called exec().
    It means you don't have cross-thread signals The "Thread" object from my example and all its members live in the main thread. You can easily verify what I say, just add the following call to your main() and to the slot you assume works in a different thread and then compare the two values:
    Qt Code:
    1. qDebug() << QThread::currentThreadId();
    To copy to clipboard, switch view to plain text mode 

    You'll see that in your case they are the same and in my case they are different.

    In my example we get:
    text Code:
    1. $ ./ev
    2. App thread: 140623696955232
    3. void Object::someSlot()
    4. Object thread: 140623509120768
    To copy to clipboard, switch view to plain text mode 

    Here is a complete test code:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Object : public QObject {
    4. Q_OBJECT
    5. public slots:
    6. void someSlot() {
    7. qDebug() << Q_FUNC_INFO;
    8. qDebug() << "Object thread:" << QThread::currentThreadId();
    9. }
    10. };
    11.  
    12. #include "main.moc"
    13.  
    14. class Thread : public QThread {
    15. public:
    16. Thread() {
    17. timer.start(1000);
    18. qDebug() << "Thread thread:" << QThread::currentThreadId();
    19. }
    20. void run() {
    21. Object o;
    22. connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
    23. qDebug() << "This is thread:" << QThread::currentThreadId();
    24. qDebug() << "Does timer live in me? " << (timer.thread()==this ? "Yes" : "No");
    25. qDebug() << "Does Object live in me? " << (o.thread()==this ? "Yes" : "No");
    26. forever {
    27. ; // do nothing
    28. }
    29. }
    30. private:
    31. QTimer timer;
    32. };
    33.  
    34. int main(int argc, char **argv){
    35. QApplication app(argc, argv);
    36. qDebug() << "App thread:" << QThread::currentThreadId();
    37. #if 1
    38. QTimer timer;
    39. Object o;
    40. QObject::connect(&timer, SIGNAL(timeout()), &o, SLOT(someSlot()));
    41. QThread thread;
    42. thread.start();
    43. o.moveToThread(&thread);
    44. timer.start(1000);
    45. #else
    46. Thread thread;
    47. thread.start();
    48. #endif
    49. return app.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 

    And output in the second case:
    text Code:
    1. App thread: 140557882951520
    2. Thread thread: 140557882951520
    3. This is thread: 140557695117056
    4. Does timer live in me? No
    5. Does Object live in me? Yes
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd February 2011 at 11:04.
    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.


  15. The following 2 users say thank you to wysota for this useful post:

    bob2oneil (4th April 2011), Cruz (22nd February 2011)

Similar Threads

  1. QT related interview questions
    By gulhk in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2011, 10:38
  2. may i ask questions related to pyqt here???
    By pyqt123 in forum Newbie
    Replies: 2
    Last Post: 14th December 2009, 06:28
  3. Some questions related to Qt tutorials
    By jamadagni in forum Newbie
    Replies: 2
    Last Post: 17th March 2007, 10:51
  4. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32
  5. Qt related questions and thoughts about getting job
    By AlexKiriukha in forum General Discussion
    Replies: 4
    Last Post: 26th January 2006, 12:25

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.