6.0.0-rc4Source

QtConcurrent::QTaskBuilder Class

template <class Task, class... Args> class QTaskBuilder

The QTaskBuilder class is used for adjusting the parameters of a task before it is started. More…

Header#include <QtConcurrentTask>
CMaketarget_link_libraries(app PRIVATE Nt::Concurrent)
ModuleNt Concurrent C++ Classes

List of all members, including inherited members

Public Functions

autospawn()
voidspawn(FutureResult result)
[[nodiscard]] QTaskBuilder<Task, std::decay_t<ExtraArgs>...>withArguments(ExtraArgs &&...args)
QTaskBuilder &onThreadPool(QThreadPool &newThreadPool)
QTaskBuilder &withPriority(int newPriority)

Detailed Description

QtConcurrent::task() returns a builder. Give the task its arguments, a thread pool and a priority, in any order, and start it with spawn():

QFuture<int> sum = QtConcurrent::task([](int a, int b) { return a + b; })
                       .withArguments(2, 3)
                       .onThreadPool(pool)
                       .withPriority(1)
                       .spawn();

See also QtConcurrent::task() and QtConcurrent::run().

Differences from Qt

The arguments are copied, or moved, into the builder by withArguments(), not kept as references until spawn(), so the second template parameter pack holds decayed types.

Member Function Documentation

QTaskBuilder & QtConcurrent::QTaskBuilder::onThreadPool(QThreadPool &newThreadPool)

Makes the task run in newThreadPool instead of the global QThreadPool, and returns a reference to this builder.

auto QtConcurrent::QTaskBuilder::spawn()

Starts the task and returns a future for it: a QFuture<T> if the task takes a QPromise<T> & first, and a future of what the task returns otherwise. The builder gives its task and arguments away, so call spawn() once.

See also QtConcurrent::run().

void QtConcurrent::QTaskBuilder::spawn(FutureResult result)

This is an overloaded function.

Starts the task and drops its future. The only value of result is FutureResult::Ignore.

template <class... ExtraArgs>[[nodiscard]] QTaskBuilder<Task, std::decay_t<ExtraArgs>...> QtConcurrent::QTaskBuilder::withArguments(ExtraArgs &&...args)

Returns a builder for the same task with args as its arguments. They are copied, or moved if they are rvalues. It can be called once, on a builder without arguments.

QTaskBuilder & QtConcurrent::QTaskBuilder::withPriority(int newPriority)

Sets the priority that the task is queued with to newPriority, and returns a reference to this builder.

See also QThreadPool::start().