mapReduce/runAsync: Fix behavior of waitForFinished

When a QFutureInterface is constructed, it is in "NoState",
but waitForFinished only blocks if it is in "Running" state.
So we need to reportStarted actually before the thread is started
(so we report it before leaving the mapReduce/runAsync method)

Change-Id: I1cdf0d627c5a6c26797b04fd0d331cddb01d50af
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Eike Ziller
2016-01-14 15:52:49 +01:00
parent 67153b225f
commit 9493d4764b

View File

@ -512,7 +512,6 @@ void blockingMapReduce(QFutureInterface<ReduceResult> futureInterface, const Con
const ReduceFunction &reduce, const CleanUpFunction &cleanup)
{
auto state = init(futureInterface);
futureInterface.reportStarted();
mapReduceLoop(futureInterface, container, map, state, reduce);
cleanup(futureInterface, state);
if (futureInterface.isPaused())
@ -523,7 +522,6 @@ void blockingMapReduce(QFutureInterface<ReduceResult> futureInterface, const Con
template <typename ResultType, typename Function, typename... Args>
void runAsyncImpl(QFutureInterface<ResultType> futureInterface, const Function &function, const Args&... args)
{
futureInterface.reportStarted();
function(futureInterface, args...);
if (futureInterface.isPaused())
futureInterface.waitForResume();
@ -540,6 +538,7 @@ QFuture<ReduceResult> mapReduce(std::reference_wrapper<Container> containerWrapp
{
auto fi = QFutureInterface<ReduceResult>();
QFuture<ReduceResult> future = fi.future();
fi.reportStarted();
std::thread(Internal::blockingMapReduce<Container, InitFunction, MapFunction, ReduceResult, ReduceFunction, CleanUpFunction>,
fi, containerWrapper, init, map, reduce, cleanup).detach();
return future;
@ -561,6 +560,7 @@ template <typename ResultType, typename Function, typename... Args>
QFuture<ResultType> runAsync(Function &&function, Args&&... args)
{
QFutureInterface<ResultType> futureInterface;
futureInterface.reportStarted();
std::thread(Internal::runAsyncImpl<ResultType,Function,Args...>, futureInterface,
std::forward<Function>(function), std::forward<Args>(args)...).detach();
return futureInterface.future();