根据我的理解,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?请给我建议。
发布于 2016-11-08 08:04:32
在本例中,QFuture对象使用QFuture对象的将运算符转换为其模板参数类型直接返回给WordCount,该将运算符转换为其模板参数类型阻止并等待结果变得可用。
typedef QMap<QString, int> WordCount;
WordCount total = mappedReduced(files, countWords, reduce);实际上,如果调用函数blockingMappedReduced的阻塞版本或从异步mappedReduced返回QFuture对象并立即阻止返回的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 falsehttps://stackoverflow.com/questions/40479569
复制相似问题