File indexing completed on 2025-09-18 09:26:50
0001
0002
0003
0004 #ifndef QTHREADPOOL_H
0005 #define QTHREADPOOL_H
0006
0007 #include <QtCore/qglobal.h>
0008
0009 #include <QtCore/qthread.h>
0010 #include <QtCore/qrunnable.h>
0011
0012 #if QT_CORE_REMOVED_SINCE(6, 6)
0013 #include <functional>
0014 #endif
0015
0016 QT_REQUIRE_CONFIG(thread);
0017
0018 QT_BEGIN_NAMESPACE
0019
0020 class QThreadPoolPrivate;
0021 class Q_CORE_EXPORT QThreadPool : public QObject
0022 {
0023 Q_OBJECT
0024 Q_DECLARE_PRIVATE(QThreadPool)
0025 Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout)
0026 Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount)
0027 Q_PROPERTY(int activeThreadCount READ activeThreadCount)
0028 Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize)
0029 Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority)
0030 friend class QFutureInterfaceBase;
0031
0032 public:
0033 QThreadPool(QObject *parent = nullptr);
0034 ~QThreadPool();
0035
0036 static QThreadPool *globalInstance();
0037
0038 void start(QRunnable *runnable, int priority = 0);
0039 bool tryStart(QRunnable *runnable);
0040
0041 #if QT_CORE_REMOVED_SINCE(6, 6)
0042 void start(std::function<void()> functionToRun, int priority = 0);
0043 bool tryStart(std::function<void()> functionToRun);
0044 #endif
0045
0046 void startOnReservedThread(QRunnable *runnable);
0047 #if QT_CORE_REMOVED_SINCE(6, 6)
0048 void startOnReservedThread(std::function<void()> functionToRun);
0049 #endif
0050
0051 template <typename Callable, QRunnable::if_callable<Callable> = true>
0052 void start(Callable &&functionToRun, int priority = 0);
0053 template <typename Callable, QRunnable::if_callable<Callable> = true>
0054 bool tryStart(Callable &&functionToRun);
0055 template <typename Callable, QRunnable::if_callable<Callable> = true>
0056 void startOnReservedThread(Callable &&functionToRun);
0057
0058 int expiryTimeout() const;
0059 void setExpiryTimeout(int expiryTimeout);
0060
0061 int maxThreadCount() const;
0062 void setMaxThreadCount(int maxThreadCount);
0063
0064 int activeThreadCount() const;
0065
0066 void setStackSize(uint stackSize);
0067 uint stackSize() const;
0068
0069 void setThreadPriority(QThread::Priority priority);
0070 QThread::Priority threadPriority() const;
0071
0072 void reserveThread();
0073 void releaseThread();
0074
0075 void setServiceLevel(QThread::QualityOfService serviceLevel);
0076 QThread::QualityOfService serviceLevel() const;
0077
0078 QT_CORE_INLINE_SINCE(6, 8)
0079 bool waitForDone(int msecs);
0080 bool waitForDone(QDeadlineTimer deadline = QDeadlineTimer::Forever);
0081
0082 void clear();
0083
0084 bool contains(const QThread *thread) const;
0085
0086 [[nodiscard]] bool tryTake(QRunnable *runnable);
0087 };
0088
0089 template <typename Callable, QRunnable::if_callable<Callable>>
0090 void QThreadPool::start(Callable &&functionToRun, int priority)
0091 {
0092 start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
0093 }
0094
0095 template <typename Callable, QRunnable::if_callable<Callable>>
0096 bool QThreadPool::tryStart(Callable &&functionToRun)
0097 {
0098 QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
0099 if (tryStart(runnable))
0100 return true;
0101 delete runnable;
0102 return false;
0103 }
0104
0105 template <typename Callable, QRunnable::if_callable<Callable>>
0106 void QThreadPool::startOnReservedThread(Callable &&functionToRun)
0107 {
0108 startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
0109 }
0110
0111 #if QT_CORE_INLINE_IMPL_SINCE(6, 8)
0112 bool QThreadPool::waitForDone(int msecs)
0113 {
0114 return waitForDone(QDeadlineTimer(msecs));
0115 }
0116 #endif
0117
0118 QT_END_NAMESPACE
0119
0120 #endif