QtConcurrent blockingMappedReduced v.s MappedReduced
QtConcurrent blockingMappedReduced v.s MappedReduced
根据我的理解QtConcurrent::blockingMappedReduced
returns最终结果,而QtConcurrent::MappedReduced
returns一个QFuture
对象,但在这个例子中http://doc.qt.io/qt-5/qtconcurrent-wordcount-main-cpp.html我看到这样的代码:
WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce);
QtConcurrent::mappedReduced
函数也是returns最终结果。我错过了什么吗?如果这是错误的,那么使用 QtConcurrent::mappedReduced
返回的结果的正确方法是什么?在什么情况下我应该 QtConcurrent::mappedReduced
而不是 QtConcurrent::blockingMappedReduced
?请指教
在示例中 QFuture
对象被 return 直接编辑到 WordCount
使用 QFuture 对象的 conversion operator to its template parameter type 阻塞并等待结果可用。
typedef QMap<QString, int> WordCount;
WordCount total = mappedReduced(files, countWords, reduce);
实际上,如果您从异步 mappedReduced
调用函数 blockingMappedReduced
或 return QFuture
对象的阻塞版本并阻塞 return 立即编辑 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
根据我的理解QtConcurrent::blockingMappedReduced
returns最终结果,而QtConcurrent::MappedReduced
returns一个QFuture
对象,但在这个例子中http://doc.qt.io/qt-5/qtconcurrent-wordcount-main-cpp.html我看到这样的代码:
WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce);
QtConcurrent::mappedReduced
函数也是returns最终结果。我错过了什么吗?如果这是错误的,那么使用 QtConcurrent::mappedReduced
返回的结果的正确方法是什么?在什么情况下我应该 QtConcurrent::mappedReduced
而不是 QtConcurrent::blockingMappedReduced
?请指教
在示例中 QFuture
对象被 return 直接编辑到 WordCount
使用 QFuture 对象的 conversion operator to its template parameter type 阻塞并等待结果可用。
typedef QMap<QString, int> WordCount;
WordCount total = mappedReduced(files, countWords, reduce);
实际上,如果您从异步 mappedReduced
调用函数 blockingMappedReduced
或 return QFuture
对象的阻塞版本并阻塞 return 立即编辑 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