9#include <QtCore/qpointer.h>
65#ifndef QT_NO_EXCEPTIONS
69#ifndef QT_NO_EXCEPTIONS
71 qWarning(
"Qt Concurrent has caught an exception thrown from a worker thread.\n"
72 "This is not supported, exceptions thrown in worker threads must be\n"
73 "caught before control returns to Qt Concurrent.");
95 if (
page->isFinished()) {
187 return p->priority() < priority;
194 if (
page->priority() == priority && !
page->isFull()) {
195 page->push(runnable);
221 if (
page->isFinished()) {
246 auto thread = std::make_unique<QThreadPoolThread>(
this);
254 thread->runnable = runnable;
268 auto allThreadsCopy = std::exchange(
allThreads, {});
275 if (thread->isRunning()) {
276 thread->runnableReady.wakeAll();
311 while (!
page->isFinished()) {
313 if (
r &&
r->autoDelete()) {
344 if (runnable ==
nullptr)
349 if (
page->tryTake(runnable)) {
350 if (
page->isFinished()) {
351 d->queue.removeOne(
page);
370 if (!
q->tryTake(runnable))
444 d->objectName = newName;
465 Q_CONSTINIT
static QPointer<QThreadPool> theInstance;
480 Q_CONSTINIT
static QPointer<QThreadPool> guiInstance;
512 if (!
d->tryStart(runnable))
513 d->enqueueTask(runnable, priority);
556 if (
d->tryStart(runnable))
596 using namespace std::chrono;
599 return duration_cast<milliseconds>(
d->expiryTimeout).count();
625 return d->requestedMaxThreadCount;
637 d->tryToStartMoreThreads();
654 return d->activeThreadCount();
676 ++
d->reservedThreads;
723 d->threadPriority = priority;
730 return d->threadPriority;
749 --
d->reservedThreads;
750 d->tryToStartMoreThreads();
780 --
d->reservedThreads;
782 if (!
d->tryStart(runnable)) {
785 d->enqueueTask(runnable, INT_MAX);
857#include "moc_qthreadpool.cpp"
858#include "qthreadpool.moc"
QByteArray first(qsizetype n) const &
static bool closingDown()
Returns true if the application objects are being destroyed; otherwise returns false.
qsizetype size() const noexcept
void removeFirst() noexcept
bool isEmpty() const noexcept
iterator insert(qsizetype i, parameter_type t)
bool removeOne(const AT &t)
const_iterator constBegin() const noexcept
const T & constFirst() const noexcept
const_iterator constEnd() const noexcept
void unlock() noexcept
Unlocks this mutex locker.
void relock() noexcept
Relocks an unlocked mutex locker.
Mutex * mutex() const noexcept
Returns the mutex on which the QMutexLocker is operating.
void unlock() noexcept
Unlocks the mutex.
void lock() noexcept
Locks the mutex.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
QThread * thread() const
Returns the thread in which the object lives.
void objectNameChanged(const QString &objectName, QPrivateSignal)
This signal is emitted after the object's name has been changed.
void enqueue(const T &t)
Adds value t to the tail of the queue.
T dequeue()
Removes the head item in the queue and returns it.
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
virtual void run()=0
Implement this pure virtual function in your subclass.
bool contains(const T &value) const
iterator insert(const T &value)
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
bool areAllThreadsActive() const
bool tryStart(QRunnable *task)
void enqueueTask(QRunnable *task, int priority=0)
void tryToStartMoreThreads()
QList< QueuePage * > queue
std::chrono::duration< int, std::milli > expiryTimeout
QThread::Priority threadPriority
QWaitCondition noActiveThreads
QQueue< QThreadPoolThread * > expiredThreads
int activeThreadCount() const
int maxThreadCount() const
QSet< QThreadPoolThread * > allThreads
void startThread(QRunnable *runnable=nullptr)
void stealAndRunRunnable(QRunnable *runnable)
QQueue< QThreadPoolThread * > waitingThreads
static QThreadPool * qtGuiInstance()
Returns the QThreadPool instance for Qt Gui.
bool waitForDone(const QDeadlineTimer &timer)
bool tooManyThreadsActive() const
QThreadPoolThread(QThreadPoolPrivate *manager)
QThreadPoolPrivate * manager
void registerThreadInactive()
QWaitCondition runnableReady
void reserveThread()
Reserves one thread, disregarding activeThreadCount() and maxThreadCount().
void start(QRunnable *runnable, int priority=0)
Reserves a thread and uses it to run runnable, unless this thread will make the current thread count ...
static QThreadPool * globalInstance()
Returns the global QThreadPool instance.
void setExpiryTimeout(int expiryTimeout)
int activeThreadCount
the number of active threads in the thread pool.
void setMaxThreadCount(int maxThreadCount)
void startOnReservedThread(QRunnable *runnable)
Releases a thread previously reserved with reserveThread() and uses it to run runnable.
bool tryTake(QRunnable *runnable)
void setThreadPriority(QThread::Priority priority)
QThreadPool(QObject *parent=nullptr)
Constructs a thread pool with the given parent.
int maxThreadCount
the maximum number of threads used by the thread pool.
uint stackSize
the stack size for the thread pool worker threads.
void setStackSize(uint stackSize)
bool waitForDone(int msecs)
Waits up to msecs milliseconds for all threads to exit and removes all threads from the thread pool.
~QThreadPool()
Destroys the QThreadPool.
bool contains(const QThread *thread) const
bool tryStart(QRunnable *runnable)
Attempts to reserve a thread to run runnable.
void releaseThread()
Releases a thread previously reserved by a call to reserveThread().
int expiryTimeout
the thread expiry timeout value in milliseconds.
QThread::Priority threadPriority
the thread priority for new worker threads.
void start(Priority=InheritPriority)
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
void setStackSize(uint stackSize)
bool wait(QMutex *, QDeadlineTimer=QDeadlineTimer(QDeadlineTimer::Forever))
QSet< QString >::iterator it
Combined button and popup list for selecting options.
GLdouble GLdouble GLdouble GLdouble q
bool comparePriority(int priority, const QueuePage *p)
QDeadlineTimer deadline(30s)
QNetworkAccessManager manager