From c55aec2ed78a5c436a522ad571b10caaffa2295d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 30 Jun 2011 11:36:03 +0200 Subject: QFutureInterface: allow to work with a QThreadPool != globalInstance() Background: It is often necessary/advisable to schedule tasks on thread pools != globalInstance(). As Herb Sutter writes in http://www.drdobbs.com/parallel/use-thread-pools-correctly-keep-tasks-sh/216500409 and the Qt Training Material stresses, tasks you schedule on a (global) thread pool should be non-blocking, which currently rules out using any of the QtConcurrent functions for, say, file I/O. Nonetheless it's often convenient to have thread pools also for file I/O, as the thumbnail viewer exercise in the Qt Training Material shows. In this case, you'd use a dedicated thead pool, leaving the global thread pool for CPU-bound tasks. Yet, none of the QtConcurrent functions allow to pick the QThreadPool instance on which to schedule the work created with them. This patch prepares for them to do so. This is the first part of the forward-port of https://qt.gitorious.org/qt/qt/merge_requests/1281. Implement by using a new QThreadPool* member that defaults to nullptr, and adding setThreadPool to set this member, then using it in lieu of QThreadPool::globalInstance() everywhere. I chose to leave m_pool == nullptr to mean globalInstance() to avoid creating the global instance whenever a QFuture is created, even if the future represents the result of a calculation not run on the global thread pool. [ChangeLog][QtCore][QFuture] Can now be used with any QThreadPool, not just globalInstance(). Task-number: QTBUG-17220 Change-Id: I4e1dc18d55cf60141b2fa3d14e2d44a3e9e74858 Reviewed-by: Olivier Goffart --- tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index c86befdf1b..0ab1b57e31 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -80,6 +81,7 @@ private slots: void exceptions(); void nestedExceptions(); #endif + void nonGlobalThreadPool(); }; void tst_QFuture::resultStore() @@ -1444,6 +1446,53 @@ void tst_QFuture::nestedExceptions() QVERIFY(MyClass::caught); } +void tst_QFuture::nonGlobalThreadPool() +{ + static Q_CONSTEXPR int Answer = 42; + + struct UselessTask : QRunnable, QFutureInterface + { + QFuture start(QThreadPool *pool) + { + setRunnable(this); + setThreadPool(pool); + reportStarted(); + QFuture f = future(); + pool->start(this); + return f; + } + + void run() Q_DECL_OVERRIDE + { + const int ms = 100 + (qrand() % 100 - 100/2); + QThread::msleep(ms); + reportResult(Answer); + reportFinished(); + } + }; + + QThreadPool pool; + + const int numTasks = QThread::idealThreadCount(); + + QVector > futures; + futures.reserve(numTasks); + + for (int i = 0; i < numTasks; ++i) + futures.push_back((new UselessTask)->start(&pool)); + + QVERIFY(!pool.waitForDone(0)); // pool is busy (meaning our tasks did end up executing there) + + QVERIFY(pool.waitForDone(10000)); // max sleep time in UselessTask::run is 150ms, so 10s should be enough + // (and the call returns as soon as all tasks finished anyway, so the + // maximum wait time only matters when the test fails) + + Q_FOREACH (const QFuture &future, futures) { + QVERIFY(future.isFinished()); + QCOMPARE(future.result(), Answer); + } +} + #endif // QT_NO_EXCEPTIONS QTEST_MAIN(tst_QFuture) -- cgit v1.2.3