diff options
Diffstat (limited to 'src/corelib/thread/qthreadpool.cpp')
-rw-r--r-- | src/corelib/thread/qthreadpool.cpp | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 93d0ffe695..5b1e4f4c33 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -195,6 +195,11 @@ bool QThreadPoolPrivate::tryStart(QRunnable *task) ++activeThreads; thread->runnable = task; + + // Ensure that the thread has actually finished, otherwise the following + // start() has no effect. + thread->wait(); + Q_ASSERT(thread->isFinished()); thread->start(); return true; } @@ -412,7 +417,7 @@ void QThreadPoolPrivate::stealAndRunRunnable(QRunnable *runnable) \ingroup thread - QThreadPool manages and recyles individual QThread objects to help reduce + QThreadPool manages and recycles individual QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global QThreadPool object, which can be accessed by calling globalInstance(). @@ -597,8 +602,12 @@ bool QThreadPool::tryStart(std::function<void()> functionToRun) return false; QRunnable *runnable = QRunnable::create(std::move(functionToRun)); + Q_ASSERT(runnable->ref == 0); + ++runnable->ref; if (d->tryStart(runnable)) return true; + --runnable->ref; + Q_ASSERT(runnable->ref == 0); delete runnable; return false; } @@ -788,6 +797,7 @@ bool QThreadPool::contains(const QThread *thread) const const QThreadPoolThread *poolThread = qobject_cast<const QThreadPoolThread *>(thread); if (!poolThread) return false; + QMutexLocker locker(&d->mutex); return d->allThreads.contains(const_cast<QThreadPoolThread *>(poolThread)); } |