ascetic652 ascetic652 - 5 months ago 34
C++ Question

QtConcurrent blockingMappedReduced v.s MappedReduced

From my understanding

returns the final results, whereas
returns a
object, but in this example I saw code like this:

WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce);

function also returns the final results. Am I missing something? If this is wrong, what is the correct way to use the results returned by
? And under what condition I should
instead of
? Please advise.


In the example QFuture object is returned directly to WordCount using QFuture object's conversion operator to its template parameter type which blocks and waits for the result to become available.

typedef QMap<QString, int> WordCount;
WordCount total = mappedReduced(files, countWords, reduce);

In practice, it's the same if you call blocking version of the function blockingMappedReduced or return QFuture object from asynchronous mappedReduced and block the returned QFuture object immediately. Note that calling result() or resultAt(0) also blocks.

WordCount total = blockingMappedReduced(files, countWords, reduce);

QFuture<WordCount> future = mappedReduced(files, countWords, reduce);
WordCount total = future.result();

If you want to interact with the QFuture object (pause, resume, check if results ready) then you can handle it asynchronously by calling mappedReduced and not using the blocking functions.

QFuture<WordCount> future = mappedReduced(files, countWords, reduce);
qDebug() << future.isResultReadyAt(0); // returns false