Atomic

class Atomic<T>(initValue: T)

A coroutine-safe wrapper around a mutable value, protected by a Mutex.

Provides suspending functions for reading and writing the value with mutual exclusion, as well as a non-locking value property for quick reads (volatile visibility only).

Parameters

initValue

the initial value

Type Parameters

T

the type of the wrapped value

See also

Constructors

Link copied to clipboard
constructor(initValue: T)

Properties

Link copied to clipboard
val value: T

The current value. Reads are volatile but not mutex-protected.

Functions

Link copied to clipboard
inline suspend fun setWithLock(owner: Any? = null, action: (T) -> T): T

Atomically updates the value by applying action to the current value while holding the Mutex.

Link copied to clipboard
inline suspend fun <V> withLock(owner: Any? = null, action: T.() -> V): V

Executes action on the current value while holding the Mutex and returns the result.