Skip to content

TaskRunner

#include <mrx/utils/TaskRunner.hpp>
class mrx::utils::TaskRunner

Inherits: QObject

Executes queued tasks sequentially using QtConcurrent worker threads.

TaskRunner accepts member functions, free functions, and generic callable wrappers, then runs them one at a time in submission order. Although only one queued task is active at a time, each task executes on a worker thread, so any shared state touched by those tasks must still be synchronized appropriately by the caller.

ReturnNameDescription
TaskRunner explicitConstructs a task runner with an optional QObject parent.
voidstartStarts processing the queued tasks if execution is not already active.
voidadd inlineEnqueues a member function to be executed later.
voidaddEnqueues a free or static function to be executed later.
voidaddEnqueues an arbitrary callable wrapper to be executed later.

explicit
explicit TaskRunner(QObject* parent = nullptr)

Constructs a task runner with an optional QObject parent.

NameTypeDescription
parentQObject*Parent object used for normal Qt ownership semantics.

void start()

Starts processing the queued tasks if execution is not already active.

Calling this function when work is already in progress should leave the existing execution flow unchanged.


inline
template<typename T> inline void add(T* context, void(T::*)() func)

Enqueues a member function to be executed later.

If either pointer is null, the request is ignored so the queue only stores valid callable work items.

NameTypeDescription
contextT*Object instance on which the member function will be invoked.
funcvoid(T::*)()Pointer to the zero-argument member function to execute.

void add(void(*)() func)

Enqueues a free or static function to be executed later.

NameTypeDescription
funcvoid(*)()Pointer to the zero-argument function to execute.

void add(std::function<void()> lambda)

Enqueues an arbitrary callable wrapper to be executed later.

NameTypeDescription
lambdastd::function<void()>Callable object to execute when it reaches the front of the queue.
ReturnNameDescription
voidfinishedEmitted after the final queued task completes and the queue becomes empty.

void finished()

Emitted after the final queued task completes and the queue becomes empty.