Task class
The library's base class for tasks.
On this page
- Use
import ThinkMeta.ConcurrentTasks.Tasks.Task;- Namespace
ThinkMeta::ConcurrentTasks::Tasks- Inherits
ITask- Platforms
- Windows, Linux
Syntax
Remarks
Derive from it and implement ITask::Run. It can be started at once or by time, and tells an optional INotifyTaskStateChange when it has exited. The object is owned by the caller and must outlive the task: from Schedule until OnTaskExited has been called (e.g. wait for it with a WaitableTask or a Synchronization::TaskGroup).
See also
Constructors
Taskprotected
Constructs the task.
Syntax
explicit Task(INotifyTaskStateChange* notifyTaskStateChange = nullptr) noexcept;Parameters
notifyTaskStateChangeINotifyTaskStateChange*- Told when the task exits; may be
nullptr.Default:nullptr
Methods
OnTaskExited
Called by the core when the task has exited.
Syntax
void OnTaskExited() noexcept override;Remarks
Tells the notify callback, if any. From then on the notified party may destroy or reuse the task object.
See also
SetNotifyCallback
Sets the object OnTaskExited forwards to.
Syntax
void SetNotifyCallback(
INotifyTaskStateChange* notifyTaskStateChange
) noexcept override;Parameters
notifyTaskStateChangeINotifyTaskStateChange*- The observer, borrowed: it must stay valid until the task has exited.
nullptr: none.
Remarks
Replaces the one given to the constructor (Synchronization::TaskGroup uses it to count its tasks). Set it before the task is scheduled.
GetControlData
Returns the task's own control data.
Syntax
TaskControlData* GetControlData() noexcept override;Return value
Type: TaskControlData*
Storage inside this object; never nullptr.
Remarks
So Task objects can be started by time.
Schedule
Adds the task to the calling task's scheduler.
Syntax
void Schedule(TaskPriority taskPriority = TaskPriority::Default) noexcept;Parameters
taskPriorityTaskPriority- Priority the task runs with;
TaskPriority::Defaultinherits the calling task's priority.Default:TaskPriority::Default
Remarks
Uses ITaskScheduler::AddTask. Call it from a task running on a scheduler; from a plain or converted thread call AddTask on the scheduler object instead.
See also
Adds the task to the given scheduler.
Syntax
void Schedule(
ITaskScheduler& scheduler,
TaskPriority taskPriority = TaskPriority::Default
) noexcept;Parameters
schedulerITaskScheduler&- The scheduler to run the task on.
taskPriorityTaskPriority- Priority the task runs with;
TaskPriority::Defaultinherits the calling task's priority.Default:TaskPriority::Default
Remarks
Uses ITaskScheduler::AddTask; any thread or task. Use it to hand work to another scheduler, e.g. from a task of a dispatcher scheduler (a UI event handler) to the worker threads of a default scheduler: there the calling task's own scheduler, which Schedule uses, is the UI thread.
Adds the task to the calling task's scheduler after a delay.
Syntax
bool Schedule(TaskPriority taskPriority, Duration delay) noexcept;Parameters
taskPriorityTaskPriority- Priority the task runs with;
TaskPriority::Defaultinherits the calling task's priority. delayDuration- Time from now until the task is queued, as a
Duration(e.g.Duration::Milliseconds).
Return value
Type: bool
true if the task is waiting to start; false if the task cannot be started by time (see ITaskScheduler::AddTimedTask).
Remarks
Needs EnableTimers. Called again before the task has started, it moves the start. On false the task was not added: it neither runs nor exits, so OnTaskExited is not called. Usage rule: a task already counted into a Synchronization::TaskGroup stays counted and the group's Wait never returns, unless you start it without delay (Schedule with the priority only) or report its exit yourself by calling its OnTaskExited, which counts it out of the group.
See also
Cancel
Asks the task to cancel.
Syntax
bool Cancel() noexcept;Return value
Type: bool
true if the request is new; false if the task was asked already.
Remarks
A task that has not started yet, also one waiting to start by time, exits without running (OnTaskExited); a running one sees the request and ends itself. Never blocks.
See also
ScheduleNow
Starts a task scheduled by time now instead of later.
Syntax
bool ScheduleNow() noexcept;Return value
Type: bool
true if the task was queued now; false if it was not waiting to start.
Remarks
For a task started by Schedule with a delay.
See also
SleepFor
Lets the calling task sleep.
Syntax
static bool SleepFor(Duration duration) noexcept;Parameters
durationDuration- Minimum time to sleep, as a
Duration(e.g.Duration::Milliseconds).
Return value
Type: bool
true after the sleep; false at once without EnableTimers.
Remarks
The thread runs other tasks meanwhile.
See also
Create
Returns a LambdaTask that runs a callable.
Syntax
template <std::invocable F>
static auto Create(
F&& func,
INotifyTaskStateChange* notifyTaskStateChange = nullptr
) noexcept;Template parameters
Fstd::invocable- Type of the callable: invocable without arguments; it must not throw.
Parameters
funcF&&- The callable; stored by value (copied or moved).
notifyTaskStateChangeINotifyTaskStateChange*- Told when the task exits; borrowed, must stay valid until then. May be
nullptr.Default:nullptr
Return value
Type: auto
The unscheduled task, a LambdaTask of the decayed callable type.
Remarks
The task is returned by value and is not scheduled yet: keep it in a variable that outlives the task and call Schedule or ITaskScheduler::AddTask on it. References captured by func must stay valid until the task has exited.
GetNotifyCallbackprotected
Returns the object that is told when the task exits.
Syntax
INotifyTaskStateChange* GetNotifyCallback() const noexcept;Return value
Type: INotifyTaskStateChange*
The notify callback, or nullptr if none is set.
Remarks
For an OnTaskExited override that must not touch the task any more after releasing its own waiters: read the callback first, then release, then notify.
Generated from ThinkMeta.ConcurrentTasks.API @ c7663c4 (2026-11-19).