QTaskBuilder::spawn: add an overload that doesn't return a future object
Fixes: QTBUG-83175 Change-Id: Idf85e47a2732742884272200d5c753805eaa640b Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
c028cbccc2
commit
678b9f78a5
@ -125,3 +125,7 @@ QtConcurrent::task([]{ return 42; }).onThreadPool(pool).spawn();
|
|||||||
//! [10]
|
//! [10]
|
||||||
QtConcurrent::task([]{ return 42; }).withPriority(10).spawn();
|
QtConcurrent::task([]{ return 42; }).withPriority(10).spawn();
|
||||||
//! [10]
|
//! [10]
|
||||||
|
|
||||||
|
//! [11]
|
||||||
|
QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn(FutureResult::Ignore);
|
||||||
|
//! [11]
|
||||||
|
@ -52,6 +52,8 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
namespace QtConcurrent {
|
namespace QtConcurrent {
|
||||||
|
|
||||||
|
enum class FutureResult { Ignore };
|
||||||
|
|
||||||
using InvokeResultType = int;
|
using InvokeResultType = int;
|
||||||
|
|
||||||
template <class Task, class ...Args>
|
template <class Task, class ...Args>
|
||||||
@ -61,6 +63,8 @@ public:
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
QFuture<InvokeResultType> spawn();
|
QFuture<InvokeResultType> spawn();
|
||||||
|
|
||||||
|
void spawn(FutureResult);
|
||||||
|
|
||||||
template <class ...ExtraArgs>
|
template <class ...ExtraArgs>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
QTaskBuilder<Task, ExtraArgs...> withArguments(ExtraArgs &&...args);
|
QTaskBuilder<Task, ExtraArgs...> withArguments(ExtraArgs &&...args);
|
||||||
@ -78,6 +82,8 @@ public:
|
|||||||
|
|
||||||
namespace QtConcurrent {
|
namespace QtConcurrent {
|
||||||
|
|
||||||
|
enum class FutureResult { Ignore };
|
||||||
|
|
||||||
template <class Task, class ...Args>
|
template <class Task, class ...Args>
|
||||||
class QTaskBuilder
|
class QTaskBuilder
|
||||||
{
|
{
|
||||||
@ -89,6 +95,12 @@ public:
|
|||||||
->start(startParameters);
|
->start(startParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void spawn(FutureResult)
|
||||||
|
{
|
||||||
|
(new StoredFunctionCall<Task, Args...>(std::move(taskWithArgs)))
|
||||||
|
->start(startParameters);
|
||||||
|
}
|
||||||
|
|
||||||
template <class ...ExtraArgs>
|
template <class ...ExtraArgs>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr auto withArguments(ExtraArgs &&...args)
|
constexpr auto withArguments(ExtraArgs &&...args)
|
||||||
|
@ -44,6 +44,13 @@
|
|||||||
This is a non-blocking call. The task might not start immediately.
|
This is a non-blocking call. The task might not start immediately.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn template <class Task, class ...Args> void QtConcurrent::QTaskBuilder<Task, Args...>::spawn(QtConcurrent::FutureResult)
|
||||||
|
|
||||||
|
Runs the task in a separate thread. This is a non-blocking call.
|
||||||
|
The task might not start immediately.
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <class Task, class ...Args> template <class ...ExtraArgs> [[nodiscard]] QTaskBuilder<Task, ExtraArgs...> QtConcurrent::QTaskBuilder<Task, Args...>::withArguments(ExtraArgs &&...args)
|
\fn template <class Task, class ...Args> template <class ...ExtraArgs> [[nodiscard]] QTaskBuilder<Task, ExtraArgs...> QtConcurrent::QTaskBuilder<Task, Args...>::withArguments(ExtraArgs &&...args)
|
||||||
|
|
||||||
@ -80,3 +87,15 @@
|
|||||||
The real implementation also contains a compile-time check for
|
The real implementation also contains a compile-time check for
|
||||||
whether the task can be invoked with the specified arguments or not.
|
whether the task can be invoked with the specified arguments or not.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\enum QtConcurrent::FutureResult
|
||||||
|
|
||||||
|
This enum type is used to invoke a special overload of
|
||||||
|
QtConcurrent::QTaskBuilder::spawn(QtConcurrent::FutureResult)
|
||||||
|
that doesn't return a future object.
|
||||||
|
|
||||||
|
\value Ignore
|
||||||
|
An auxiliary tag which introduced to improve code
|
||||||
|
readability.
|
||||||
|
*/
|
||||||
|
@ -143,6 +143,11 @@
|
|||||||
You can set the priority for a task:
|
You can set the priority for a task:
|
||||||
|
|
||||||
\snippet code/src_concurrent_qtconcurrenttask.cpp 10
|
\snippet code/src_concurrent_qtconcurrenttask.cpp 10
|
||||||
|
|
||||||
|
If you don't need a future object, you can call
|
||||||
|
QtConcurrent::QTaskBuilder::spawn(QtConcurrent::FutureResult::Ignore):
|
||||||
|
|
||||||
|
\snippet code/src_concurrent_qtconcurrenttask.cpp 11
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -43,6 +43,7 @@ private Q_SLOTS:
|
|||||||
void useCustomThreadPool();
|
void useCustomThreadPool();
|
||||||
void setPriority();
|
void setPriority();
|
||||||
void adjustAllSettings();
|
void adjustAllSettings();
|
||||||
|
void ignoreFutureResult();
|
||||||
};
|
};
|
||||||
|
|
||||||
using namespace QtConcurrent;
|
using namespace QtConcurrent;
|
||||||
@ -155,6 +156,20 @@ void tst_QtConcurrentTask::adjustAllSettings()
|
|||||||
|
|
||||||
QCOMPARE(result, QVector<int>({1, 2, 3}));
|
QCOMPARE(result, QVector<int>({1, 2, 3}));
|
||||||
}
|
}
|
||||||
|
void tst_QtConcurrentTask::ignoreFutureResult()
|
||||||
|
{
|
||||||
|
QThreadPool pool;
|
||||||
|
|
||||||
|
std::atomic_int value = 0;
|
||||||
|
for (std::size_t i = 0; i < 10; ++i)
|
||||||
|
task([&value]{ ++value; })
|
||||||
|
.onThreadPool(pool)
|
||||||
|
.spawn(FutureResult::Ignore);
|
||||||
|
|
||||||
|
pool.waitForDone();
|
||||||
|
|
||||||
|
QCOMPARE(value, 10);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(tst_QtConcurrentTask)
|
QTEST_MAIN(tst_QtConcurrentTask)
|
||||||
#include "tst_qtconcurrenttask.moc"
|
#include "tst_qtconcurrenttask.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user