Results 1 to 11 of 11

Thread: How to ensure threads run() function executes first

  1. #1
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to ensure threads run() function executes first

    Hi ,

    I have the below code where I am using threads **started signal** to ensure run function of the clientThread executes first and then any other function. All initialization are done in the run function and if any other function (serverConnect fn as below) executes first it creates issues.
    But using logs I see that 1 in 10 times run function of clientThread executes after serverConnect().

    Thank you,

    Qt Code:
    1. void Base::drpThreadStarted()
    2. {
    3. /* Once DRP thread is started , start the client thread. */
    4. connect(clientThread,SIGNAL(started()),this,SLOT(clientThreadStarted()));
    5. clientThread->start();
    6. }
    7.  
    8.  
    9. void Base::clientThreadStarted()
    10. {
    11. /* Connect to the notification server. */
    12. clientThread->serverConnect();
    13.  
    14. /* Connect to the message queue in watchdog. */
    15. clientThread->watchdogConnect();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

  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: How to ensure threads run() function executes first

    But using logs I see that 1 in 10 times run function of clientThread executes after serverConnect().
    That doesn't have to mean this is the real order of things, rather, that is the order of when the log messages were logged, which is very different, and you have to consider this possibility in a threaded environment.
    Just for sake of testing, you can set a flag in your QThread::run() method, and read it back in Base::clientThreadStarted() - see if the flag is ever not set when clientThreadStarted() runs.
    ==========================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
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to ensure threads run() function executes first

    Got it resolved, Thanks.

  4. #4
    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: How to ensure threads run() function executes first

    Got it resolved, Thanks.
    Would be nice if you could share how, so others reading the thread later with a similar problem could benefit 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.

  5. #5
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to ensure threads run() function executes first

    I modified the run function to include a single shot timer, the timeout Signal is connected to Slot function which will call the connect functions:
    Qt Code:
    1. void ClientThread::run()
    2. {
    3.  
    4. /*Initialize the connect to servers timer*/
    5. conn_to_server_timer = new QTimer;
    6. connect(conn_to_server_timer, SIGNAL(timeout()), this, SLOT(clientThreadStarted()), Qt::QueuedConnection);
    7. conn_to_server_timer->setInterval(100);
    8. conn_to_server_timer->setSingleShot(TRUE);
    9. conn_to_server_timer->start(); // puts one event in the threads event queue
    10.  
    11. exec(); // puts the thread in event loop
    12. }
    13.  
    14. void ClientThread::clientThreadStarted()
    15. {
    16. /* Connect to the notification server. */
    17. serverConnect();
    18.  
    19. /* Connect to the message queue in watchdog. */
    20. watchdogConnect();
    21. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: How to ensure threads run() function executes first

    This is not really a solution, its a hack, which happens to work for you on the specific setup you have.
    There is no guarantee that on another system, the interval you set will be sufficient.
    Since you have't posted any relevant code (I assume the run() you posted is not complete, as only putting the connect statements in a thread makes little sense) , I can't offer a better solution.
    ==========================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
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to ensure threads run() function executes first

    what I understand from how QT works, the exec() function of the QT puts the thread into event loop and then only thread starts processing all events, also timeout event of "conn_to_server_timer" timer.
    So even if timer times out before exec() function, slot function will only be executed by exec(). Also my goal was to make sure connect to server happens after run().
    I don't think its a hack, its just the serialization.

  8. #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: How to ensure threads run() function executes first

    what I understand from how QT works, the exec() function of the QT puts the thread into event loop and then only thread starts processing all events,
    The exec() you call within QThread::run() starts the threads own event loop - it NOT the applications event loop.

    So even if timer times out before exec() function, slot function will only be executed by exec().
    Hmm...
    I'd ask for a second opinion on this.
    I am not sure, but if exec() starts the event loop, and the timer fires before the event loop started, where will this event go? the event loop did not start yet.
    It could be that even if the event loop is not running it will queue events (but not process them) but I never checked it - you might want to check it.

    Also my goal was to make sure connect to server happens after run().
    After run() has existed, or after run() has been called (but not necessarily finished)?
    If its the former, then you could simply connect to the finished() signal of the thread.
    It its the later, simply send a signal from run() and connect to it.

    If run is only doing initialization, you could pack it in is own class, and move that object to the thread - without the need to subclass QThread - from a design perspective its a better approach.

    I don't think its a hack, its just the serialization.
    Anything relaying on an arbitrary timeout to execute "in the right order" in a threaded environment - is a hack.
    ==========================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.

  9. #9
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to ensure threads run() function executes first

    The only valid concern I can see is "if exec() starts the event loop, and the timer fires before the event loop started, where will this event go? the event loop did not start yet".

    Ok you mean to say just after initialization of members, I can emit a signal and in the handler I can connect to the servers? Also I don't want anything to do on thread exit, so do not need finished signal.

  10. #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: How to ensure threads run() function executes first

    Ok you mean to say just after initialization of members, I can emit a signal and in the handler I can connect to the servers?
    Well yes, which at the end is exatcly what should have been connecting to the QThread::started() signal.

    Also I don't want anything to do on thread exit, so do not need finished signal.
    From the code you posted it does seem that way though since you have no other code in your run().

    To be frank, for me at least you posted to little information and too little code to know where or even what the problem is.
    You have to remember, that what yo are talking about in terms of what you see as behavior, is not the problem, but the symptom.
    To understand the problem, more code is needed, especially when we talk about a threaded setup.
    ==========================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. #11
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to ensure threads run() function executes first

    I very well know and wish I could post more code , but because of some constraints I cannot.

    Anyways, Thank you for help!

Similar Threads

  1. Call function of object within Threads.
    By marcos.miranda in forum Newbie
    Replies: 16
    Last Post: 17th January 2017, 12:25
  2. Replies: 5
    Last Post: 7th November 2016, 19:02
  3. JS not executes in QTWebkit
    By ericzhang in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2014, 11:31
  4. Dialog executes in debug build but not release
    By awhite1159 in forum Qt Programming
    Replies: 5
    Last Post: 24th June 2008, 17:51
  5. Plugin ctor executes, but not dtor ?
    By scollyer in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2008, 14:18

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.