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#include <functional>
13
14QT_REQUIRE_CONFIG(thread);
15
16QT_BEGIN_NAMESPACE
17
18class QThreadPoolPrivate;
19class Q_CORE_EXPORT QThreadPool : public QObject
20{
21 Q_OBJECT
22 Q_DECLARE_PRIVATE(QThreadPool)
23 Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout)
24 Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount)
25 Q_PROPERTY(int activeThreadCount READ activeThreadCount)
26 Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize)
27 Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority)
28 friend class QFutureInterfaceBase;
29
30public:
31 QThreadPool(QObject *parent = nullptr);
32 ~QThreadPool();
33
34 static QThreadPool *globalInstance();
35
36 void start(QRunnable *runnable, int priority = 0);
37 bool tryStart(QRunnable *runnable);
38
39#if QT_CORE_REMOVED_SINCE(6, 6)
40 void start(std::function<void()> functionToRun, int priority = 0);
41 bool tryStart(std::function<void()> functionToRun);
42#endif
43
44 void startOnReservedThread(QRunnable *runnable);
45#if QT_CORE_REMOVED_SINCE(6, 6)
46 void startOnReservedThread(std::function<void()> functionToRun);
47#endif
48
49 template <typename Callable, QRunnable::if_callable<Callable> = true>
50 void start(Callable &&functionToRun, int priority = 0);
51 template <typename Callable, QRunnable::if_callable<Callable> = true>
52 bool tryStart(Callable &&functionToRun);
53 template <typename Callable, QRunnable::if_callable<Callable> = true>
54 void startOnReservedThread(Callable &&functionToRun);
55
56 int expiryTimeout() const;
57 void setExpiryTimeout(int expiryTimeout);
58
59 int maxThreadCount() const;
60 void setMaxThreadCount(int maxThreadCount);
61
62 int activeThreadCount() const;
63
64 void setStackSize(uint stackSize);
65 uint stackSize() const;
66
67 void setThreadPriority(QThread::Priority priority);
68 QThread::Priority threadPriority() const;
69
70 void reserveThread();
71 void releaseThread();
72
73 bool waitForDone(int msecs = -1);
74
75 void clear();
76
77 bool contains(const QThread *thread) const;
78
79 [[nodiscard]] bool tryTake(QRunnable *runnable);
80};
81
82template <typename Callable, QRunnable::if_callable<Callable>>
83void QThreadPool::start(Callable &&functionToRun, int priority)
84{
85 start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
86}
87
88template <typename Callable, QRunnable::if_callable<Callable>>
89bool QThreadPool::tryStart(Callable &&functionToRun)
90{
91 QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
92 if (tryStart(runnable))
93 return true;
94 delete runnable;
95 return false;
96}
97
98template <typename Callable, QRunnable::if_callable<Callable>>
99void QThreadPool::startOnReservedThread(Callable &&functionToRun)
100{
101 startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
102}
103
104QT_END_NAMESPACE
105
106#endif
107

source code of qtbase/src/corelib/thread/qthreadpool.h