diff options
author | Corentin Jabot <corentinjabot@gmail.com> | 2013-07-27 23:02:48 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-16 15:43:53 +0200 |
commit | 0d8ba7e34974fe9c1a60977a10d34413650b33b0 (patch) | |
tree | f3c0856e465ca16ca2969b5ad06f12c19e3ff06d /tests/auto/corelib/thread | |
parent | 8b86443e36bc3102e97be4f40f381151d19fd1a5 (diff) |
QThreadPool - Add method clear() to remove queued QRunnable.
QThreadPool::clear() method removes all queued QRunnable.
When a large number of long-running tasks are queud in a
QThreadPool its destruction, which calls waitForDone(), can
be quite long.
QThreadPool:clear() removes (and deletes when appropriate)
all QRunnable that have yet to be started from the queue
enabling a faster interruption.
Change-Id: Ie5d6028ad3cfe7e439d1db068c8d0936ff818db9
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests/auto/corelib/thread')
-rw-r--r-- | tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index fb34afb880..10a59ac301 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -97,6 +97,7 @@ private slots: void priorityStart_data(); void priorityStart(); void waitForDone(); + void clear(); void waitForDoneTimeout(); void destroyingWaitsForTasksToFinish(); void stressTest(); @@ -855,6 +856,34 @@ void tst_QThreadPool::waitForDoneTimeout() QVERIFY(threadPool.waitForDone(400)); } +void tst_QThreadPool::clear() +{ + QSemaphore sem(0); + class BlockingRunnable : public QRunnable + { + public: + QSemaphore & sem; + BlockingRunnable(QSemaphore & sem) : sem(sem){} + void run() + { + sem.acquire(); + count.ref(); + } + }; + + QThreadPool threadPool; + threadPool.setMaxThreadCount(10); + int runs = 2 * threadPool.maxThreadCount(); + count.store(0); + for (int i = 0; i <= runs; i++) { + threadPool.start(new BlockingRunnable(sem)); + } + threadPool.clear(); + sem.release(threadPool.maxThreadCount()); + threadPool.waitForDone(); + QCOMPARE(count.load(), threadPool.maxThreadCount()); +} + void tst_QThreadPool::destroyingWaitsForTasksToFinish() { QTime total, pass; |