Skip to content

Atomic

Spice
import "std/os/atomic";

Atomic<T> struct

A wrapper that guards a value of arbitrary type with a mutex, so it can be read and updated safely from multiple threads.

Constructors

ctor

Spice
public p Atomic.ctor()

Construct an atomic initialized with the zero value of its type

ctor

Spice
public p Atomic.ctor(T value)

Construct an atomic initialized with the given value

Parameters

Name Type Description
value T Initial value

Methods

store

Spice
public p Atomic.store(const T& value)

Atomically store a new value

Parameters

Name Type Description
value const T& Value to store

load

Spice
public const f<const T&> Atomic.load()

Atomically load the current value

Returns: const T& — Reference to the current value

exchange

Spice
public f<T> Atomic.exchange(const T& value)

Atomically replace the value and return the previous one

Parameters

Name Type Description
value const T& New value to store

Returns: T — Previous value

compareExchange

Spice
public f<bool> Atomic.compareExchange(const T& expected, const T& desired)

Atomically set the value to desired only if it currently equals expected

Parameters

Name Type Description
expected const T& Value the atomic is compared against
desired const T& Value to store if the comparison succeeds

Returns: bool — true if the value was exchanged, false otherwise