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

Make waitForFinished() wait for reportStarted() too, or provide overload or a new method

    XMLWordPrintable

Details

    • User Story
    • Resolution: Done
    • P2: Important
    • 6.0
    • 6.0
    • Core: Threads
    • None
    • 13
    • Qt6_Foundation_Sprint 16

    Description

      Consider the following code:

          QPromise<int> promise;
          QFuture<int> future = promise.future();
          QPointer<QThread> thread(QThread::create([] (QPromise<int> promise) {
              promise.reportStarted(); 
              promise.addResult(42);
      	promise.reportFinished();  // notifies QFuture that the computation is finished
          }, std::move(promise)));
          thread->start();
          future.waitForFinished();
      

      It can happen, that waitForFinished() may be hit before executing reportStarted() in the working thread. Currently, if reportStarted() was not called before waitForFinished(), the waitForFinished() call won't block. In this case the above code won't work as expected.

      That's why we need to wait also for reportStarted() inside waitForFinished(). If we don't want to do behavioral change, we may optionally provide an overload for waitForFinished() or provide a new method.

      The idea came from: https://codereview.qt-project.org/c/qt/qtbase/+/302631

      Attachments

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

        Activity

          People

            sonakur Sona Kurazyan
            jkobus Jarek Kobus
            Maurice Kalinowski Maurice Kalinowski
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes