diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index f4dc940cc4b..3a9c0569e7c 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -113,7 +113,12 @@ void QThreadPoolThread::run() manager->waitingThreads.enqueue(this); registerThreadInactive(); // wait for work, exiting after the expiry timeout is reached - runnableReady.wait(locker.mutex(), QDeadlineTimer(manager->expiryTimeout)); + QDeadlineTimer deadline; + if (manager->expiryTimeout.count() < 0) + deadline = QDeadlineTimer::Forever; + else + deadline.setRemainingTime(manager->expiryTimeout); + runnableReady.wait(locker.mutex(), deadline); // this thread is about to be deleted, do not work or expire if (!manager->allThreads.contains(this)) { Q_ASSERT(manager->queue.isEmpty()); diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index 2006016d478..6decea9b939 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -414,6 +414,21 @@ void tst_QThreadPool::expiryTimeout() threadPool.setExpiryTimeout(expiryTimeout); QCOMPARE(threadPool.expiryTimeout(), expiryTimeout); + + threadPool.waitForDone(); + // Negative times should be 'forever' + threadPool.setExpiryTimeout(-1); + QCOMPARE(threadPool.expiryTimeout(), -1); + + threadPool.start(&task); + QVERIFY(task.semaphore.tryAcquire(1, 10'000)); + QCOMPARE(task.runCount.loadRelaxed(), 3); + firstThread = task.thread; + + QTest::qWait(100); // Let some time elapse after the task finishes... + + // Since the thread never expires it should still be waiting for a new task: + QVERIFY(firstThread->isRunning()); } void tst_QThreadPool::expiryTimeoutRace() // QTBUG-3786