From 17c96dec2a16db0994df7980d511dbff83a5041d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Wed, 24 Jun 2009 15:18:54 +0200 Subject: Improve QThreadPool scalability. Reduce lock contention: - Skip locking in the accessor functions (where we can) - exit early in tryStart (before locking the mutex) when the threadpool is running at max capacity. --- src/corelib/concurrent/qthreadpool.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/corelib/concurrent/qthreadpool.cpp b/src/corelib/concurrent/qthreadpool.cpp index 83374da..9c53b7e 100644 --- a/src/corelib/concurrent/qthreadpool.cpp +++ b/src/corelib/concurrent/qthreadpool.cpp @@ -222,6 +222,8 @@ void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority) int QThreadPoolPrivate::activeThreadCount() const { + // To improve scalability this function is called without holding + // the mutex lock -- keep it thread-safe. return (allThreads.count() - expiredThreads.count() - waitingThreads @@ -481,6 +483,12 @@ bool QThreadPool::tryStart(QRunnable *runnable) return false; Q_D(QThreadPool); + + // To improve scalability perform a check on the thread count + // before locking the mutex. + if (d->allThreads.isEmpty() == false && d->activeThreadCount() >= d->maxThreadCount) + return false; + QMutexLocker locker(&d->mutex); return d->tryStart(runnable); } @@ -527,7 +535,6 @@ void QThreadPool::setExpiryTimeout(int expiryTimeout) int QThreadPool::maxThreadCount() const { Q_D(const QThreadPool); - QMutexLocker locker(&d->mutex); return d->maxThreadCount; } @@ -556,7 +563,6 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount) int QThreadPool::activeThreadCount() const { Q_D(const QThreadPool); - QMutexLocker locker(&d->mutex); return d->activeThreadCount(); } -- cgit v0.12