diff options
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qfuture.qdoc | 7 | ||||
-rw-r--r-- | src/corelib/thread/qfuture_impl.h | 14 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool.cpp | 17 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool_p.h | 5 |
4 files changed, 25 insertions, 18 deletions
diff --git a/src/corelib/thread/qfuture.qdoc b/src/corelib/thread/qfuture.qdoc index 8fec4989bb..335c16a39c 100644 --- a/src/corelib/thread/qfuture.qdoc +++ b/src/corelib/thread/qfuture.qdoc @@ -33,8 +33,6 @@ \ingroup thread - To start a computation, use one of the APIs in the \l {Qt Concurrent} framework. - QFuture allows threads to be synchronized against one or more results which will be ready at a later point in time. The result can be of any type that has default, copy and possibly move constructors. If @@ -134,7 +132,10 @@ you can attach multiple continuations to a signal, which are invoked in the same thread or a new thread. - \sa QtFuture::connect(), QFutureWatcher, {Qt Concurrent} + \note To start a computation and store results in a QFuture, use QPromise or + one of the APIs in the \l {Qt Concurrent} framework. + + \sa QPromise, QtFuture::connect(), QFutureWatcher, {Qt Concurrent} */ /*! \fn template <typename T> QFuture<T>::QFuture() diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index 4998fa79aa..4d77acb0fe 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -724,34 +724,34 @@ static QFuture<ArgsType<Signal>> connect(Sender *sender, Signal signal) if constexpr (std::is_void_v<ArgsType>) { connections->first = QObject::connect(sender, signal, sender, [promise, connections]() mutable { - promise.reportFinished(); QObject::disconnect(connections->first); QObject::disconnect(connections->second); + promise.reportFinished(); }); } else if constexpr (QtPrivate::isTupleV<ArgsType>) { connections->first = QObject::connect(sender, signal, sender, [promise, connections](auto... values) mutable { - promise.reportResult(std::make_tuple(values...)); - promise.reportFinished(); QObject::disconnect(connections->first); QObject::disconnect(connections->second); + promise.reportResult(std::make_tuple(values...)); + promise.reportFinished(); }); } else { connections->first = QObject::connect(sender, signal, sender, [promise, connections](ArgsType value) mutable { - promise.reportResult(value); - promise.reportFinished(); QObject::disconnect(connections->first); QObject::disconnect(connections->second); + promise.reportResult(value); + promise.reportFinished(); }); } connections->second = QObject::connect(sender, &QObject::destroyed, sender, [promise, connections]() mutable { - promise.reportCanceled(); - promise.reportFinished(); QObject::disconnect(connections->first); QObject::disconnect(connections->second); + promise.reportCanceled(); + promise.reportFinished(); }); return promise.future(); diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 6d258af9df..7eb1b601ff 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -176,7 +176,7 @@ bool QThreadPoolPrivate::tryStart(QRunnable *task) } // can't do anything if we're over the limit - if (activeThreadCount() >= maxThreadCount) + if (activeThreadCount() >= maxThreadCount()) return false; if (waitingThreads.count() > 0) { @@ -249,7 +249,7 @@ void QThreadPoolPrivate::tryToStartMoreThreads() bool QThreadPoolPrivate::tooManyThreadsActive() const { const int activeThreadCount = this->activeThreadCount(); - return activeThreadCount > maxThreadCount && (activeThreadCount - reservedThreads) > 1; + return activeThreadCount > maxThreadCount() && (activeThreadCount - reservedThreads) > 1; } /*! @@ -571,7 +571,7 @@ bool QThreadPool::tryStart(std::function<void()> functionToRun) Q_D(QThreadPool); QMutexLocker locker(&d->mutex); - if (!d->allThreads.isEmpty() && d->activeThreadCount() >= d->maxThreadCount) + if (!d->allThreads.isEmpty() && d->activeThreadCount() >= d->maxThreadCount()) return false; QRunnable *runnable = QRunnable::create(std::move(functionToRun)); @@ -612,7 +612,9 @@ void QThreadPool::setExpiryTimeout(int expiryTimeout) /*! \property QThreadPool::maxThreadCount - \brief the maximum number of threads used by the thread pool. + \brief the maximum number of threads used by the thread pool. This property + will default to the value of QThread::idealThreadCount() at the moment the + QThreadPool object is created. \note The thread pool will always use at least 1 thread, even if \a maxThreadCount limit is zero or negative. @@ -623,7 +625,7 @@ void QThreadPool::setExpiryTimeout(int expiryTimeout) int QThreadPool::maxThreadCount() const { Q_D(const QThreadPool); - return d->maxThreadCount; + return d->requestedMaxThreadCount; } void QThreadPool::setMaxThreadCount(int maxThreadCount) @@ -631,10 +633,10 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount) Q_D(QThreadPool); QMutexLocker locker(&d->mutex); - if (maxThreadCount == d->maxThreadCount) + if (maxThreadCount == d->requestedMaxThreadCount) return; - d->maxThreadCount = maxThreadCount; + d->requestedMaxThreadCount = maxThreadCount; d->tryToStartMoreThreads(); } @@ -756,6 +758,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)); } diff --git a/src/corelib/thread/qthreadpool_p.h b/src/corelib/thread/qthreadpool_p.h index 4d73a480b5..a2c7d917a4 100644 --- a/src/corelib/thread/qthreadpool_p.h +++ b/src/corelib/thread/qthreadpool_p.h @@ -55,6 +55,7 @@ #include "QtCore/qmutex.h" #include "QtCore/qthread.h" #include "QtCore/qwaitcondition.h" +#include "QtCore/qthreadpool.h" #include "QtCore/qset.h" #include "QtCore/qqueue.h" #include "private/qobject_p.h" @@ -158,6 +159,8 @@ public: void tryToStartMoreThreads(); bool tooManyThreadsActive() const; + int maxThreadCount() const + { return qMax(requestedMaxThreadCount, 1); } // documentation says we start at least one void startThread(QRunnable *runnable = nullptr); void reset(); bool waitForDone(int msecs); @@ -174,7 +177,7 @@ public: QWaitCondition noActiveThreads; int expiryTimeout = 30000; - int maxThreadCount = QThread::idealThreadCount(); + int requestedMaxThreadCount = QThread::idealThreadCount(); // don't use this directly int reservedThreads = 0; int activeThreads = 0; uint stackSize = 0; |