Results 1 to 10 of 10

Thread: QTimer and main eventLoop is there possible race condition?

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTimer and main eventLoop is there possible race condition?

    Hi all,

    I'm not sure if such a situation creates race conditions and thus, should I take care about it?
    I have QTimer which invokes some action periodically, every 100ms, this action can sometime take more than 100ms to complete, and also during this action I can stop QTimer (like timer->stop() ) - so in that case, when timer will be stoped during this, lets say 150ms, to complete my task, and after this eventLoop will start, will it produce event saying QTimer timeout, or not?

    best regards
    Marek

  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: QTimer and main eventLoop is there possible race condition?

    Race condition is something else, not what you describe.
    In general, if you have a closed loop (I mean without calling processEvents() in it) then while the loop runs no signal slots will be processes, thus if you stop your timer over a slot connection, it wont be evoked before your loop ends.
    If you want to be able to stop your timer before your loop ends, you will have to call processEvents() in 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. #3
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer and main eventLoop is there possible race condition?

    Thanks,

    So in other words, QTimer object does not generate event (timeout) to main event loop by itself, and only during processing of main event loop, active QTimer's are checked if any of them reached timeout, and then event is generated, right ?

    Marek

  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: QTimer and main eventLoop is there possible race condition?

    No.
    QTimer DOES generate an event to the main loop, and the main loop has to process it - but if the main loop does not get CPU time to process it, it doesn't get processed. This situation occurs in busy loops inside the main event loop (your main application thread).
    So if you have a slot running a busy loop in the main thread, you have to call processEvents() to give the main loop chance to process any pending events - or use threads.
    However when you are looking at response resolution of few ms, you will have to live with a timing margin in both solutions.
    ==========================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 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: QTimer and main eventLoop is there possible race condition?

    Actually it depends what you mean. Timers as such do not generate events but are rather checked as part of the event loop whether they reached their timeout. If so, then the application will generate a timer event for the object interested in the timer's timeout (be it the timer object itself or the QObject instance calling startTimer() or using QBasicTimer).

    So to sum things up, this will execute the slot only once:

    Qt Code:
    1. connect(&t, SIGNAL(timeout()), ..., ...);
    2. t.start(1000); // 1s
    3. sleep(100); // 100s
    4. qApp->processEvents();// lets the timeout be checked and generates an event for the 't' object that will eventually call the slot from line #2
    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.


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

    franki (5th July 2012)

  7. #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: QTimer and main eventLoop is there possible race condition?

    So to sum things up, this will execute the slot only once:
    after 100s.
    ==========================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. The following user says thank you to high_flyer for this useful post:

    franki (5th July 2012)

  9. #7
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer and main eventLoop is there possible race condition?

    So what I'm really asking is:
    if we put line:
    t.stop()
    after line #4 and before line #5 - will there be any timeout event in main loop waiting to be processed, or there won't be any event because it is generated on the fly when main loop is processed.

    Thanks
    Marek

  10. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer and main eventLoop is there possible race condition?

    Come on, it's a 5-line program. It is taking you longer to write these posts than it would to simply type in those 5 lines and try it yourself. Once you've done that, you can report your results here.

  11. The following user says thank you to d_stranz for this useful post:

    franki (5th July 2012)

  12. #9
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer and main eventLoop is there possible race condition?

    You right

    timeout event is not generated in that case. I did it in separate thread, but probably it doesn't matter.

    Marek

  13. #10
    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: QTimer and main eventLoop is there possible race condition?

    Quote Originally Posted by franki View Post
    So what I'm really asking is:
    if we put line:

    after line #4 and before line #5 - will there be any timeout event in main loop waiting to be processed, or there won't be any event because it is generated on the fly when main loop is processed.
    It's just an academic discussion. The timer can timeout at any point in time close to what the timer is set for depending on many aspects you have little control over. You should always assume the timer fires if it was meant to fire. In your example if you call stop() as part of another timer's timer event (or slot) then whether this timer fires depends on whether it is earlier in the timer list than the timer you're currently processing. You should never expect to stop a timer that is intended to fire NOW. Then you won't have to ask yourself such questions.

    BTW. Regarding your last statement -- using threads might matter.
    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. Replies: 0
    Last Post: 11th December 2011, 22:51
  2. Replies: 0
    Last Post: 14th April 2011, 15:10
  3. Problem with eventloop in QThread
    By speedy_gonzales in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 19:17
  4. 2D Race Car collision detection
    By neonnds in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2008, 08:10
  5. Thread eventLoop and run
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:36

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.