Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:40

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 
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 #include <functional>
0013 
0014 QT_REQUIRE_CONFIG(thread);
0015 
0016 QT_BEGIN_NAMESPACE
0017 
0018 class QThreadPoolPrivate;
0019 class Q_CORE_EXPORT QThreadPool : public QObject
0020 {
0021     Q_OBJECT
0022     Q_DECLARE_PRIVATE(QThreadPool)
0023     Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout)
0024     Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount)
0025     Q_PROPERTY(int activeThreadCount READ activeThreadCount)
0026     Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize)
0027     Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority)
0028     friend class QFutureInterfaceBase;
0029 
0030 public:
0031     QThreadPool(QObject *parent = nullptr);
0032     ~QThreadPool();
0033 
0034     static QThreadPool *globalInstance();
0035 
0036     void start(QRunnable *runnable, int priority = 0);
0037     bool tryStart(QRunnable *runnable);
0038 
0039 #if QT_CORE_REMOVED_SINCE(6, 6)
0040     void start(std::function<void()> functionToRun, int priority = 0);
0041     bool tryStart(std::function<void()> functionToRun);
0042 #endif
0043 
0044     void startOnReservedThread(QRunnable *runnable);
0045 #if QT_CORE_REMOVED_SINCE(6, 6)
0046     void startOnReservedThread(std::function<void()> functionToRun);
0047 #endif
0048 
0049     template <typename Callable, QRunnable::if_callable<Callable> = true>
0050     void start(Callable &&functionToRun, int priority = 0);
0051     template <typename Callable, QRunnable::if_callable<Callable> = true>
0052     bool tryStart(Callable &&functionToRun);
0053     template <typename Callable, QRunnable::if_callable<Callable> = true>
0054     void startOnReservedThread(Callable &&functionToRun);
0055 
0056     int expiryTimeout() const;
0057     void setExpiryTimeout(int expiryTimeout);
0058 
0059     int maxThreadCount() const;
0060     void setMaxThreadCount(int maxThreadCount);
0061 
0062     int activeThreadCount() const;
0063 
0064     void setStackSize(uint stackSize);
0065     uint stackSize() const;
0066 
0067     void setThreadPriority(QThread::Priority priority);
0068     QThread::Priority threadPriority() const;
0069 
0070     void reserveThread();
0071     void releaseThread();
0072 
0073     bool waitForDone(int msecs = -1);
0074 
0075     void clear();
0076 
0077     bool contains(const QThread *thread) const;
0078 
0079     [[nodiscard]] bool tryTake(QRunnable *runnable);
0080 };
0081 
0082 template <typename Callable, QRunnable::if_callable<Callable>>
0083 void QThreadPool::start(Callable &&functionToRun, int priority)
0084 {
0085     start(QRunnable::create(std::forward<Callable>(functionToRun)), priority);
0086 }
0087 
0088 template <typename Callable, QRunnable::if_callable<Callable>>
0089 bool QThreadPool::tryStart(Callable &&functionToRun)
0090 {
0091     QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun));
0092     if (tryStart(runnable))
0093         return true;
0094     delete runnable;
0095     return false;
0096 }
0097 
0098 template <typename Callable, QRunnable::if_callable<Callable>>
0099 void QThreadPool::startOnReservedThread(Callable &&functionToRun)
0100 {
0101     startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun)));
0102 }
0103 
0104 QT_END_NAMESPACE
0105 
0106 #endif