Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qthreadpool.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTHREADPOOL_H
5#define QTHREADPOOL_H
6
7#include <QtCore/qglobal.h>
8
9#include <QtCore/qthread.h>
10#include <QtCore/qrunnable.h>
11
12#if QT_CORE_REMOVED_SINCE(6, 6)
13#include <functional>
14#endif
15
17
18QT_BEGIN_NAMESPACE
19
20class QThreadPoolPrivate;
21class Q_CORE_EXPORT QThreadPool : public QObject
22{
23 Q_OBJECT
24 Q_DECLARE_PRIVATE(QThreadPool)
25 Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout)
26 Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount)
27 Q_PROPERTY(int activeThreadCount READ activeThreadCount)
28 Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize)
29 Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority)
30 friend class QFutureInterfaceBase;
31
32public:
33 QThreadPool(QObject *parent = nullptr);
34 ~QThreadPool();
35
36 static QThreadPool *globalInstance();
37
38 void start(QRunnable *runnable, int priority = 0);
39 bool tryStart(QRunnable *runnable);
40
41#if QT_CORE_REMOVED_SINCE(6, 6)
42 void start(std::function<void()> functionToRun, int priority = 0);
43 bool tryStart(std::function<void()> functionToRun);
44#endif
45
46 void startOnReservedThread(QRunnable *runnable);
47#if QT_CORE_REMOVED_SINCE(6, 6)
48 void startOnReservedThread(std::function<void()> functionToRun);
49#endif
50
51 template <typename Callable, QRunnable::if_callable<Callable> = true>
52 void start(Callable &&functionToRun, int priority = 0);
53 template <typename Callable, QRunnable::if_callable<Callable> = true>
54 bool tryStart(Callable &&functionToRun);
55 template <typename Callable, QRunnable::if_callable<Callable> = true>
56 void startOnReservedThread(Callable &&functionToRun);
57
58 int expiryTimeout() const;
59 void setExpiryTimeout(int expiryTimeout);
60
61 int maxThreadCount() const;
62 void setMaxThreadCount(int maxThreadCount);
63
64 int activeThreadCount() const;
65
66 void setStackSize(uint stackSize);
67 uint stackSize() const;
68
69 void setThreadPriority(QThread::Priority priority);
70 QThread::Priority threadPriority() const;
71
72 void reserveThread();
73 void releaseThread();
74
75 void setServiceLevel(QThread::QualityOfService serviceLevel);
76 QThread::QualityOfService serviceLevel() const;
77
78 QT_CORE_INLINE_SINCE(6, 8)
79 bool waitForDone(int msecs);
80 bool waitForDone(QDeadlineTimer deadline = QDeadlineTimer::Forever);
81
82 void clear();
83
84 bool contains(const QThread *thread) const;
85
86 [[nodiscard]] bool tryTake(QRunnable *runnable);
87};
88
89template <typename Callable, QRunnable::if_callable<Callable>>
90void QThreadPool::start(Callable &&functionToRun, int priority)
91{
92 start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
93}
94
95template <typename Callable, QRunnable::if_callable<Callable>>
96bool QThreadPool::tryStart(Callable &&functionToRun)
97{
98 QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
99 if (tryStart(runnable))
100 return true;
101 delete runnable;
102 return false;
103}
104
105template <typename Callable, QRunnable::if_callable<Callable>>
106void QThreadPool::startOnReservedThread(Callable &&functionToRun)
107{
108 startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
109}
110
111#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
112bool QThreadPool::waitForDone(int msecs)
113{
114 return waitForDone(QDeadlineTimer(msecs));
115}
116#endif
117
118QT_END_NAMESPACE
119
120#endif
void run() override
QThreadPoolPrivate * manager
void registerThreadInactive()
QRunnable * runnable
QWaitCondition runnableReady
\inmodule QtCore
Definition qthreadpool.h:22
bool isFinished()
int priority() const
bool comparePriority(int priority, const QueuePage *p)
QT_REQUIRE_CONFIG(thread)