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

QFuture::then functor needs access to its promise

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 6.10
    • Core: Threads
    • None

      As pointed in https://bugreports.qt.io/browse/QTBUG-140786 the continuation functor needs access to either its QPromise or some sort of std::stop_token otherwise is impossible to know if the chain was canceled in the functor.

       Mandatory code snippet

      //...
      auto future = someFutureWork().then([&](QPromise<void> &myPromise, const QFuture<int> &previousResults) {  // either (optionally) pass QPromise
          Q_UNUSED(previousResults);
          for (int i = 0; i < (1 << 31) && !myPromise.isCanceled(); ++i) {
            QThread::msleep(100); // simulate some heavy work    }
          myPromise.finish();
      }).then([&](std::stop_token stopToken) { // or use std::stop_token, though it's a C++20 feature ...
          for (int i = 0; i < (1 << 31) && !stopToken.stop_requested(); ++i) {
             QThread::msleep(100); // simulate some heavy work
           }
      });
      
      future.cancelChain();
      
      //...
      

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

            cnn Qt Core & Network
            taipan BogDan Vatra
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes