根据我的理解,QtConcurrent::blockingMappedReduced返回最终结果,而QtConcurrent::MappedReduced返回QFuture对象,但是在此示例http://doc.qt.io/qt-5/qtconcurrent-wordcount-main-cpp.html中,我看到了这样的代码:

WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce);
QtConcurrent::mappedReduced函数还返回最终结果。我想念什么吗?如果这是错误的,使用QtConcurrent::mappedReduced返回的结果的正确方法是什么?在什么情况下我应该使用QtConcurrent::mappedReduced而不是QtConcurrent::blockingMappedReduced?请指教。

最佳答案

在示例中,使用QFuture对象的conversion operator to its template parameter typeQFuture对象直接返回到WordCount,该模块阻止并等待结果变为可用。

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

在实践中,如果您调用函数blockingMappedReduced的阻止版本或从异步QFuture返回mappedReduced对象并立即阻止返回的QFuture对象,则是相同的。请注意,调用result()resultAt(0)也会阻止。
WordCount total = blockingMappedReduced(files, countWords, reduce);

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

如果要与QFuture对象进行交互(暂停,恢复,检查结果是否准备就绪),则可以通过调用mappedReduced而不使用阻塞函数来异步处理它。
QFuture<WordCount> future = mappedReduced(files, countWords, reduce);
qDebug() << future.isResultReadyAt(0); // returns false

关于c++ - Qt并发阻塞MappedReduced与MappedReduced,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40479569/

10-08 21:43