multithreadingqtdictionarythreadpoolqtconcurrent

Multiple QtConcurrent::map execution order


Lets say I have a vector of 100 elements and Func1 and Func2. In the single threaded version Func1 process vector elements and when Func1 finishes, Func2 must start a different process on elements. I'm curious to know if I utilize QtConcurrent::map in the following order, in which order actually Func1 and Func2 will execute?

QFuture<void> future;
future = QtConcurrent::map(vector, Func1);
future = QtConcurrent::map(vector, Func2);

I must mention that using future.waitForFinished() will block my application main thread which I don't want.

future = QtConcurrent::map(vector, Func1);
future.waitForFinished();
future = QtConcurrent::map(vector, Func2);

Also I don't want to execute those QtConcurrent::map in a secondary thread and do the future.waitForFinished() there, because in that approach I will lose one of my threads in threadpool. So, the question is do tasks added by QtConcurrent::map execute in order?

EDIT

In both single threaded and multi-threaded approaches Func2 must run only after Func1 finishes processing all elements.


Solution

  • Since you want all calls to Func1 to complete before any calls to Func2 are made you can't make the second call to QtConcurrent::map before the first is known to have finished.

    However, rather than calling future.waitForFinished() you can use a QFutureWatcher...

    QFutureWatcher<void> watcher;
    auto future = QtConcurrent::map(vector, Func1);
    QObject::connect(&watcher, &QFutureWatcher::finished,
                     [&]()
                       {
    
                         /*
                          * All calls to Func1 have finished so it's
                          * safe to invoke Func2.
                          */
                         future = QtConcurrent::map(vector, Func2);
                       });
    watcher.setFuture(future);
    

    The above is untested but hopefully gives you some idea of what's required.