Skip to content

Atomics

Atomic Variable Declaration

Declare a variable with atomic semantics:

atomic int counter = 0; // sequential consistency (default)
atomic(relaxed) int flag = 0; // explicit ordering

Atomic Expression

A single statement executed atomically:

atomic counter = counter + 1; // atomic load + store
atomic(release) ready = 1; // release store

Atomic Block

All loads and stores inside the block are atomic:

atomic
{
counter = counter + 1;
total = total + value;
}
atomic(acquire)
{
int val = sharedData; // acquire load
}

Memory Ordering

ModeKeywordDescription
Sequentially Consistentseqcst (default)Full ordering, strongest guarantee
AcquireacquireReads cannot be reordered before this load
ReleasereleaseWrites cannot be reordered after this store
Acquire-ReleaseacqrelCombined acquire + release
RelaxedrelaxedOnly atomicity, no ordering guarantee

Compound Assignment

Compound operators emit single locked instructions (no separate load-op-store):

atomic counter += 1; // LOCK ADD
atomic flags &= mask; // LOCK AND
atomic bits |= flag; // LOCK OR
atomic value ^= toggle; // LOCK XOR
atomic count -= 1; // LOCK SUB

Qualifier Chaining

atomic combines with other qualifiers in any order:

atomic unsafe { ... } // atomic + unsafe
lock(m) atomic { ... } // lock + atomic (skips try/catch)
unsafe atomic { ... } // order doesn't matter

When lock wraps an atomic block, the try/catch wrapper is skipped for performance since atomic blocks cannot throw.