Preliminary. ThinkMeta ConcurrentTasks and this documentation are preliminary; everything is subject to change.

API reference / Synchronization

CriticalSection class

A recursive mutual-exclusion lock owned by a task (not by a thread).

Use
import ThinkMeta.ConcurrentTasks.Synchronization.CriticalSection;
Namespace
ThinkMeta::ConcurrentTasks::Synchronization
Inherits
Suspendable

Syntax

using CriticalSection = _CriticalSection<SuspensionKind::Multiple>;
using CriticalSection_s = _CriticalSection<SuspensionKind::Single>;

Template parameters

_SuspensionKindSuspensionKind
SuspensionKind::Multiple: any number of contending tasks; SuspensionKind::Single: at most one suspended contender at a time, cheaper.

Remarks

A task that holds it may enter again and must leave as often as it entered. A contending task is suspended, not blocked, so its thread runs other tasks. Comes as CriticalSection (any number of contending tasks) and CriticalSection_s (one contending task). All members identify the caller as the current task, so they must be called from a task or from an OS thread converted with ConvertThreadToTask - also TryEnter and Leave. Leave must be called by the owning task. On Leave the lock is handed directly to a suspended task (it becomes the owner before it runs); waiters are served LIFO, no fairness. No timeouts. CriticalSection_s: at most one task may be suspended in Enter at a time (the owner plus one contender). Destroy the critical section only when it is free and no task can enter or leave it any more.

See also

CriticalSectionLock, SpinCriticalSectionLock

Names

NameDescription
CriticalSection Recursive task lock for any number of contending tasks.
CriticalSection_s Recursive task lock for at most one suspended contender at a time.

Cheaper than CriticalSection.

Constructors

CriticalSection

Creates a free critical section.

Syntax

CriticalSection() noexcept = default;

Methods

TryEnter

Enters if the lock is free or already held by the calling task.

Syntax

[[nodiscard]] bool TryEnter() noexcept;

Return value

Type: bool

true if entered (lock count incremented); false if another task holds it.

Remarks

Never suspends.

See also

Enter, Leave

Enter

Enters the lock, waiting for it if necessary.

Syntax

void Enter() noexcept;

Remarks

Suspends the calling task while another task holds the lock. Recursive. CriticalSection_s may spin briefly (YieldProcessor) during a hand-off instead.

See also

TryEnter, Leave

Leave

Leaves the lock once.

Syntax

void Leave() noexcept;

Remarks

The last Leave hands the lock to a suspended task or frees it. Must be called by the owning task (asserted in Debug). Never suspends.

See also

Enter

Generated from ThinkMeta.ConcurrentTasks.API @ 137ec1d (2026-10-17).