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

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

    XMLWordPrintable

Details

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

    Description

      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; // <----
          }
      ...
      }
      

      Attachments

        Issue Links

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

          Activity

            People

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

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes