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

QSingleShotTimer may unnecessarily create a timer in the current thread

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4: Low
    • Resolution: Unresolved
    • Affects Version/s: 5.10.1
    • Fix Version/s: None
    • Component/s: Core: Event loop
    • Labels:
      None

      Description

      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.

        Attachments

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

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            kuba Kuba Ober
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes