Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-31 08:27:32

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