WaitableTask class
A Task that others can wait for until it has exited.
- Use
import ThinkMeta.ConcurrentTasks.Tasks.WaitableTask;- Namespace
ThinkMeta::ConcurrentTasks::Tasks- Inherits
Task- Platforms
- Windows, Linux
Syntax
using WaitableTask = _WaitableTask<SuspensionKind::Multiple>;
using WaitableTask_s = _WaitableTask<SuspensionKind::Single>;Template parameters
_SuspensionKindSuspensionKindSuspensionKind::Multiple: any number of waiting tasks;SuspensionKind::Single: at most one waiting task at a time, cheaper, with timeouts.
Remarks
Wait suspends the caller until the task's ITask::Run has returned and OnTaskExited has been called. Derive from it and implement Run. WaitableTask allows any number of waiting tasks; WaitableTask_s allows at most one task waiting at a time, is cheaper and accepts a timeout. The waiting side must be a task or a thread converted with ConvertThreadToTask. After the first exit every Wait returns at once: an object is meant to run once. Lifetime: with WaitableTask_s the waiting task may destroy the object as soon as its Wait returned, since the exit's Set is one operation. With WaitableTask it may not: the exit stores the signal and then takes the list of waiters, and a waiter that finds the signal passes in between (see Synchronization::ManualResetEvent); destroy it only when no task waits on it any more and the exit has returned - or wait for the task through a Synchronization::TaskGroup, which covers this case.
See also
Names
| Name | Description |
|---|---|
WaitableTask |
WaitableTask for any number of waiting tasks.
|
WaitableTask_s |
WaitableTask for at most one waiting task at a time.
Cheaper, and |
Constructors
WaitableTaskprotected
Constructs the task.
Syntax
explicit WaitableTask(
INotifyTaskStateChange* notifyTaskStateChange = nullptr
) noexcept;Parameters
notifyTaskStateChangeINotifyTaskStateChange*- Told when the task exits; may be
nullptr.Default:nullptr
Methods
Wait
Suspends the calling task until this task has exited.
Syntax
void Wait() noexcept;Remarks
Returns at once if it has exited already. A CancelTask of the waiting task does not end it.
See also
_s only
Suspends the calling task until this task has exited, with a timeout.
Syntax
[[nodiscard]] WaitStatus Wait(Duration timeout) noexcept;Parameters
timeoutDuration- Longest time to wait, as a
Duration;Duration::Infinitewaits without a timeout.
Return value
Type: WaitStatus
WaitStatus::Signaled if the task has exited; WaitStatus::TimedOut if the wait timed out; WaitStatus::Cancelled if the waiting task was cancelled (CancelTask).
Remarks
WaitableTask_s only.
See also
TryWait
Returns whether the task has exited, without waiting.
Syntax
[[nodiscard]] bool TryWait() noexcept;Return value
Type: bool
true if the task has exited; otherwise false.
See also
OnTaskExited
Called by the core when the task has exited.
Syntax
void OnTaskExited() noexcept override;Remarks
Releases the waiting tasks, then tells the notify callback (e.g. a Synchronization::TaskGroup), if any. An override must call this version and must not touch the object after it.
Create
Returns a waitable task 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 WaitableLambdaTask of this suspension kind and the decayed callable type.
Remarks
Hides Task::Create, which returns a task that cannot be waited for. Keep the task in a variable that outlives it, schedule it and wait for it: auto task = WaitableTask_s::Create(f); task.Schedule(scheduler); task.Wait(); References captured by func must stay valid until the task has exited.
Generated from ThinkMeta.ConcurrentTasks.API @ c7663c4 (2026-11-19).