Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-66458

QSingleShotTimer may unnecessarily create a timer in the current thread



    • Bug
    • Resolution: Unresolved
    • P4: Low
    • None
    • 5.10.1
    • Core: Event loop
    • None


      QSingleShotTimer::QSingleShotTimer attempts to create a timer in the current thread even if the receiver thread is different than current thread. The timer will of course be moved immediately to the target thread, but this makes QTimer::singleShot unusable for cross-thread code invocation in source threads that lack an event loop, even if the destination thread has an event loop. I.e. it won't work in a generic std::thread.

      This issue came up when folks were looking for an idiomatic way of posting a callable to an object living in another thread. QTimer::singleShot is almost the solution, but unfortunately doesn't work when there's no event loop on the call stack at the invocation time.

      See this SO question for currently available workarounds. The best I could think of was to add the call to `QEvent` destructor and post to an object in the target thread - see my answer there. It would be cool if such hacks were not necessary.


        No reviews matched the request. Check your Options in the drop-down menu of this sections header.



            Unassigned Unassigned
            kuba Kuba Ober
            2 Vote for this issue
            3 Start watching this issue



              Gerrit Reviews

                There are no open Gerrit changes