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

Windows: QThread::terminate() provokes a hang sometimes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 6.2.4
    • Core: Threads
    • None
    • Windows

      Found this while investigating QTBUG-103922:

      Sample code:

      for (int i = 0; i < 1000; ++i)
      {
          QThread thread;
          thread.start();
          thread.terminate();
          thread.wait();
      }
      

      Sometimes this causes a hung at least on Windows - probably due to some internal race condition.
      Pay attention that terminate() sometimes may have no any effect here, it just returns early because termination is not yet enabled in QThreadPrivate::start().

      see src/corelib/thread/qthread_win.cpp:

      unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(void *arg) noexcept
      {
      ...
          QThread::setTerminationEnabled(false); 
      
      ...
          QThread::setTerminationEnabled(true);
      ...
      }
      
      void QThread::terminate()
      {
      ...
          if (!d->terminationEnabled) {
              d->terminatePending = true;
              return; // <----
          }
      ...
      }
      

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

            thiago Thiago Macieira
            studiosus Vladimir Belyavsky
            Votes:
            3 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes