4

From my understanding QtConcurrent::blockingMappedReduced returns the final results, whereas QtConcurrent::MappedReduced returns a QFuture object, but in this example http://doc.qt.io/qt-5/qtconcurrent-wordcount-main-cpp.html I saw code like this:

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

QtConcurrent::mappedReduced 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 QtConcurrent::mappedReduced? And under what condition I should QtConcurrent::mappedReduced instead of QtConcurrent::blockingMappedReduced? Please advise.

demonplus
  • 5,613
  • 12
  • 49
  • 68
ascetic652
  • 472
  • 1
  • 5
  • 18
  • I guess that example code uses [the implicit conversion of QFuture to its underlying type](http://doc.qt.io/qt-5/qfuture.html#operator-T) which would be the same as to call `WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce).result();` – Dmitry Nov 08 '16 at 07:44

1 Answers1

4

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
talamaki
  • 5,324
  • 1
  • 27
  • 40
  • Can you explain what conversion operator a little bit more? I read the documentation and found no explanation there. @talamaki – ascetic652 Nov 08 '16 at 15:17
  • You can define a member function of a class, called a conversion function, that converts from the type of its class to another specified type. – talamaki Nov 08 '16 at 18:27
  • See [this](http://stackoverflow.com/questions/1307876/how-do-conversion-operators-work-in-c/) for more details – talamaki Nov 08 '16 at 18:33
  • Can you explain a little bit what does future.isResultReadyAt(0) mean? @talamaki – ascetic652 Nov 13 '16 at 06:34