diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-11-21 15:51:34 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-01-31 19:20:43 +0100 |
commit | c76dd72dc6b78c34edb4dfc5196bb93a51cc488f (patch) | |
tree | 9a5539a13c95a6f61bdb2e9e07bd7df12daff0e1 | |
parent | 6c4e0f2d2de5f877f698a0faa7bd0d027b7320c9 (diff) |
Add a constructor for QRunnable from anonymous functions
This makes it easier to create one without having to create
a derivative class. The patch also adds a path to avoid using
QRunnable directly in QThreadPool.
Change-Id: I9caa7dabb6f641b547d4771c863aa6ab7f01b704
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
-rw-r--r-- | src/corelib/thread/qrunnable.cpp | 27 | ||||
-rw-r--r-- | src/corelib/thread/qrunnable.h | 2 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool.cpp | 32 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool.h | 5 | ||||
-rw-r--r-- | tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp | 23 |
5 files changed, 83 insertions, 6 deletions
diff --git a/src/corelib/thread/qrunnable.cpp b/src/corelib/thread/qrunnable.cpp index bd0a32b53d..58a764d407 100644 --- a/src/corelib/thread/qrunnable.cpp +++ b/src/corelib/thread/qrunnable.cpp @@ -114,4 +114,31 @@ QRunnable::~QRunnable() \sa autoDelete(), QThreadPool */ +class FunctionRunnable : public QRunnable +{ + std::function<void()> m_functor; +public: + FunctionRunnable(std::function<void()> functor) : m_functor(std::move(functor)) + { + } + void run() override + { + m_functor(); + } +}; + +/*! + \since 5.15 + + Creates a QRunnable that calls \a fun in run(). + + Auto-deletion is enabled by default. + + \sa run(), autoDelete() +*/ +QRunnable *QRunnable::create(std::function<void()> fun) +{ + return new FunctionRunnable(std::move(fun)); +} + QT_END_NAMESPACE diff --git a/src/corelib/thread/qrunnable.h b/src/corelib/thread/qrunnable.h index b8e842118c..c13aa3fa8c 100644 --- a/src/corelib/thread/qrunnable.h +++ b/src/corelib/thread/qrunnable.h @@ -41,6 +41,7 @@ #define QRUNNABLE_H #include <QtCore/qglobal.h> +#include <functional> QT_BEGIN_NAMESPACE @@ -59,6 +60,7 @@ public: QRunnable() : ref(0) { } virtual ~QRunnable(); + static QRunnable *create(std::function<void()> fun); bool autoDelete() const { return ref != -1; } void setAutoDelete(bool _autoDelete) { ref = _autoDelete ? 0 : -1; } diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 5f23a78c8a..d1875a69a9 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -512,6 +512,22 @@ void QThreadPool::start(QRunnable *runnable, int priority) } /*! + \overload + \since 5.15 + + Reserves a thread and uses it to run \a fun, unless this thread will + make the current thread count exceed maxThreadCount(). In that case, + \a fun is added to a run queue instead. The \a priority argument can + be used to control the run queue's order of execution. +*/ +void QThreadPool::start(std::function<void()> fun, int priority) +{ + if (!fun) + return; + start(QRunnable::create(std::move(fun)), priority); +} + +/*! Attempts to reserve a thread to run \a runnable. If no threads are available at the time of calling, then this function @@ -542,6 +558,22 @@ bool QThreadPool::tryStart(QRunnable *runnable) return d->tryStart(runnable); } +/*! + \overload + \since 5.15 + Attempts to reserve a thread to run \a fun. + + If no threads are available at the time of calling, then this function + does nothing and returns \c false. Otherwise, \a fun is run immediately + using one available thread and this function returns \c true. +*/ +bool QThreadPool::tryStart(std::function<void()> fun) +{ + if (!fun) + return false; + return tryStart(QRunnable::create(std::move(fun))); +} + /*! \property QThreadPool::expiryTimeout Threads that are unused for \a expiryTimeout milliseconds are considered diff --git a/src/corelib/thread/qthreadpool.h b/src/corelib/thread/qthreadpool.h index cd27b7c08a..2eede44eca 100644 --- a/src/corelib/thread/qthreadpool.h +++ b/src/corelib/thread/qthreadpool.h @@ -45,6 +45,8 @@ #include <QtCore/qthread.h> #include <QtCore/qrunnable.h> +#include <functional> + QT_REQUIRE_CONFIG(thread); QT_BEGIN_NAMESPACE @@ -70,6 +72,9 @@ public: void start(QRunnable *runnable, int priority = 0); bool tryStart(QRunnable *runnable); + void start(std::function<void()> fun, int priority = 0); + bool tryStart(std::function<void()> fun); + int expiryTimeout() const; void setExpiryTimeout(int expiryTimeout); diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index 60e8d8cba2..49ce918caf 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -64,6 +64,7 @@ public: private slots: void runFunction(); + void runFunction2(); void createThreadRunFunction(); void runMultiple(); void waitcomplete(); @@ -160,17 +161,27 @@ void tst_QThreadPool::runFunction() { QThreadPool manager; testFunctionCount = 0; - manager.start(createTask(noSleepTestFunction)); + manager.start(noSleepTestFunction); } QCOMPARE(testFunctionCount, 1); } +void tst_QThreadPool::runFunction2() +{ + int localCount = 0; + { + QThreadPool manager; + manager.start([&]() { ++localCount; }); + } + QCOMPARE(localCount, 1); +} + void tst_QThreadPool::createThreadRunFunction() { { QThreadPool manager; testFunctionCount = 0; - manager.start(createTask(noSleepTestFunction)); + manager.start(noSleepTestFunction); } QCOMPARE(testFunctionCount, 1); @@ -184,7 +195,7 @@ void tst_QThreadPool::runMultiple() QThreadPool manager; testFunctionCount = 0; for (int i = 0; i < runs; ++i) { - manager.start(createTask(sleepTestFunctionMutex)); + manager.start(sleepTestFunctionMutex); } } QCOMPARE(testFunctionCount, runs); @@ -193,7 +204,7 @@ void tst_QThreadPool::runMultiple() QThreadPool manager; testFunctionCount = 0; for (int i = 0; i < runs; ++i) { - manager.start(createTask(noSleepTestFunctionMutex)); + manager.start(noSleepTestFunctionMutex); } } QCOMPARE(testFunctionCount, runs); @@ -201,7 +212,7 @@ void tst_QThreadPool::runMultiple() { QThreadPool manager; for (int i = 0; i < 500; ++i) - manager.start(createTask(emptyFunct)); + manager.start(emptyFunct); } } @@ -211,7 +222,7 @@ void tst_QThreadPool::waitcomplete() const int runs = 500; for (int i = 0; i < 500; ++i) { QThreadPool pool; - pool.start(createTask(noSleepTestFunction)); + pool.start(noSleepTestFunction); } QCOMPARE(testFunctionCount, runs); } |