Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
19
21class Q_CORE_EXPORT QThreadPool : public QObject
22{
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)
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 QT_CORE_INLINE_SINCE(6, 8)
76 bool waitForDone(int msecs);
77 bool waitForDone(QDeadlineTimer deadline = QDeadlineTimer::Forever);
78
79 void clear();
80
81 bool contains(const QThread *thread) const;
82
83 [[nodiscard]] bool tryTake(QRunnable *runnable);
84};
85
86template <typename Callable, QRunnable::if_callable<Callable>>
87void QThreadPool::start(Callable &&functionToRun, int priority)
88{
89 start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
90}
91
92template <typename Callable, QRunnable::if_callable<Callable>>
93bool QThreadPool::tryStart(Callable &&functionToRun)
94{
95 QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
96 if (tryStart(runnable))
97 return true;
98 delete runnable;
99 return false;
100}
101
102template <typename Callable, QRunnable::if_callable<Callable>>
103void QThreadPool::startOnReservedThread(Callable &&functionToRun)
104{
105 startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
106}
107
108#if QT_CORE_INLINE_IMPL_SINCE(6, 8)
109bool QThreadPool::waitForDone(int msecs)
110{
111 return waitForDone(QDeadlineTimer(msecs));
112}
113#endif
114
116
117#endif
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
Definition qrunnable.h:18
static QRunnable * create(Callable &&functionToRun)
Definition qrunnable.h:114
\inmodule QtCore
Definition qthreadpool.h:22
void startOnReservedThread(QRunnable *runnable)
Releases a thread previously reserved with reserveThread() and uses it to run runnable.
bool waitForDone(int msecs)
Waits up to msecs milliseconds for all threads to exit and removes all threads from the thread pool.
bool tryStart(QRunnable *runnable)
Attempts to reserve a thread to run runnable.
b clear()
Combined button and popup list for selecting options.
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLuint start
#define QT_REQUIRE_CONFIG(feature)
#define Q_PROPERTY(...)
#define Q_OBJECT
unsigned int uint
Definition qtypes.h:34
QDeadlineTimer deadline(30s)