diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2023-04-11 11:38:37 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2023-05-04 15:05:18 +0200 |
commit | 0e7e1c33965c43932e6c0228acc2f68259533482 (patch) | |
tree | e4c4366c71ae18e7ff631e45bf3f0daff82f7736 /src/corelib/thread/qthreadpool.h | |
parent | 1403b63a5747b8210edaa3da571ae568230dc156 (diff) |
Take move-only functions for the threadpool
We never copy the function so only need it to movable. Moves the
functions to templates using the new QRunnable create version.
[ChangeLog][QtCore][QThreadPool] Methods taking callable functions,
can now take move-only lambdas.
Fixes: QTBUG-112302
Change-Id: I2cb200f0abcf7e0fdbef0457fe2a6176764ad93d
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/thread/qthreadpool.h')
-rw-r--r-- | src/corelib/thread/qthreadpool.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/corelib/thread/qthreadpool.h b/src/corelib/thread/qthreadpool.h index e6d0326c4c..a097ace14b 100644 --- a/src/corelib/thread/qthreadpool.h +++ b/src/corelib/thread/qthreadpool.h @@ -36,11 +36,22 @@ public: void start(QRunnable *runnable, int priority = 0); bool tryStart(QRunnable *runnable); +#if QT_CORE_REMOVED_SINCE(6, 6) void start(std::function<void()> functionToRun, int priority = 0); bool tryStart(std::function<void()> functionToRun); +#endif void startOnReservedThread(QRunnable *runnable); +#if QT_CORE_REMOVED_SINCE(6, 6) void startOnReservedThread(std::function<void()> functionToRun); +#endif + + template <typename Callable, QRunnable::if_callable<Callable> = true> + void start(Callable &&functionToRun, int priority = 0); + template <typename Callable, QRunnable::if_callable<Callable> = true> + bool tryStart(Callable &&functionToRun); + template <typename Callable, QRunnable::if_callable<Callable> = true> + void startOnReservedThread(Callable &&functionToRun); int expiryTimeout() const; void setExpiryTimeout(int expiryTimeout); @@ -68,6 +79,28 @@ public: [[nodiscard]] bool tryTake(QRunnable *runnable); }; +template <typename Callable, QRunnable::if_callable<Callable>> +void QThreadPool::start(Callable &&functionToRun, int priority) +{ + start(QRunnable::create(std::forward<Callable>(functionToRun)), priority); +} + +template <typename Callable, QRunnable::if_callable<Callable>> +bool QThreadPool::tryStart(Callable &&functionToRun) +{ + QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun)); + if (tryStart(runnable)) + return true; + delete runnable; + return false; +} + +template <typename Callable, QRunnable::if_callable<Callable>> +void QThreadPool::startOnReservedThread(Callable &&functionToRun) +{ + startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun))); +} + QT_END_NAMESPACE #endif |