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// Qt-Security score:significant reason:default
4
5#ifndef QTHREADPOOL_H
6#define QTHREADPOOL_H
7
8#include <QtCore/qglobal.h>
9
10#include <QtCore/qthread.h>
11#include <QtCore/qrunnable.h>
12
13#if QT_CORE_REMOVED_SINCE(6, 6)
14#include <functional>
15#endif
16
17QT_BEGIN_NAMESPACE
18
19class QThreadPoolPrivate;
20class Q_CORE_EXPORT QThreadPool : public QObject
21{
22 Q_OBJECT
23 Q_DECLARE_PRIVATE(QThreadPool)
24 Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout)
25 Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount)
26 Q_PROPERTY(int activeThreadCount READ activeThreadCount)
27 Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize)
28 Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority)
29 friend class QFutureInterfaceBase;
30
31public:
32 QThreadPool(QObject *parent = nullptr);
33 ~QThreadPool();
34
35 static QThreadPool *globalInstance();
36
37 void start(QRunnable *runnable, int priority = 0);
38 bool tryStart(QRunnable *runnable);
39
40#if QT_CORE_REMOVED_SINCE(6, 6)
41 void start(std::function<void()> functionToRun, int priority = 0);
42 bool tryStart(std::function<void()> functionToRun);
43#endif
44
45 void startOnReservedThread(QRunnable *runnable);
46#if QT_CORE_REMOVED_SINCE(6, 6)
47 void startOnReservedThread(std::function<void()> functionToRun);
48#endif
49
50 template <typename Callable, QRunnable::if_callable<Callable> = true>
51 void start(Callable &&functionToRun, int priority = 0);
52 template <typename Callable, QRunnable::if_callable<Callable> = true>
53 bool tryStart(Callable &&functionToRun);
54 template <typename Callable, QRunnable::if_callable<Callable> = true>
55 void startOnReservedThread(Callable &&functionToRun);
56
57 int expiryTimeout() const;
58 void setExpiryTimeout(int expiryTimeout);
59
60 int maxThreadCount() const;
61 void setMaxThreadCount(int maxThreadCount);
62
63 int activeThreadCount() const;
64
65 void setStackSize(uint stackSize);
66 uint stackSize() const;
67
68 void setThreadPriority(QThread::Priority priority);
69 QThread::Priority threadPriority() const;
70
71 void reserveThread();
72 void releaseThread();
73
74 void setServiceLevel(QThread::QualityOfService serviceLevel);
75 QThread::QualityOfService serviceLevel() const;
76
77 QT_CORE_INLINE_SINCE(6, 8)
78 bool waitForDone(int msecs);
79 bool waitForDone(QDeadlineTimer deadline = QDeadlineTimer::Forever);
80
81 void clear();
82
83 bool contains(const QThread *thread) const;
84
85 [[nodiscard]] bool tryTake(QRunnable *runnable);
86};
87
88template <typename Callable, QRunnable::if_callable<Callable>>
89void QThreadPool::start(Callable &&functionToRun, int priority)
90{
91 start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
92}
93
94template <typename Callable, QRunnable::if_callable<Callable>>
95bool QThreadPool::tryStart(Callable &&functionToRun)
96{
97 QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
98 if (tryStart(runnable))
99 return true;
100 delete runnable;
101 return false;
102}
103
104template <typename Callable, QRunnable::if_callable<Callable>>
105void QThreadPool::startOnReservedThread(Callable &&functionToRun)
106{
107 startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
108}
109
110#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
111bool QThreadPool::waitForDone(int msecs)
112{
113 return waitForDone(QDeadlineTimer(msecs));
114}
115#endif
116
117QT_END_NAMESPACE
118
119#endif
void run() override
QThreadPoolPrivate * manager
void registerThreadInactive()
QRunnable * runnable
QWaitCondition runnableReady
\inmodule QtCore
Definition qthreadpool.h:21
bool isFinished()
int priority() const
bool comparePriority(int priority, const QueuePage *p)