Atomic

expect class Atomic<V>(initialValue: V)(source)

Atomic value of V.

import arrow.atomic.AtomicInt
import arrow.atomic.update
import arrow.atomic.value
import arrow.fx.coroutines.parMap

suspend fun main() {
val count = AtomicInt(0)
(0 until 20_000).parMap {
count.update(Int::inc)
}
println(count.value)
}

Atomic also offers some other interesting operators such as loop, update, tryUpdate, etc.

WARNING: Use AtomicInt and AtomicLong for Int and Long on Kotlin Native!

actual class Atomic<V>(initialValue: V)(source)
actual typealias Atomic<V> = AtomicReference<V>(source)
actual class Atomic<V>(initialValue: V)(source)

Constructors

Link copied to clipboard
expect constructor(initialValue: V)
actual constructor(initialValue: V)
actual constructor(initialValue: V)

Properties

Link copied to clipboard
var <T> Atomic<T>.value: T

Functions

Link copied to clipboard
expect fun compareAndSet(expected: V, new: V): Boolean

Compare current value with expected and set to new if they're the same. Note, 'compare' is checking the actual object id, not 'equals'.

actual fun compareAndSet(expected: V, new: V): Boolean
actual fun compareAndSet(expected: V, new: V): Boolean
Link copied to clipboard
expect fun get(): V
actual fun get(): V
actual fun get(): V
Link copied to clipboard
expect fun getAndSet(value: V): V
actual fun getAndSet(value: V): V
actual fun getAndSet(value: V): V
Link copied to clipboard
inline fun <V> Atomic<V>.getAndUpdate(function: (V) -> V): V

Updates variable atomically using the specified function of its value and returns its old value.

Link copied to clipboard
inline fun <V> Atomic<V>.loop(action: (V) -> Unit): Nothing

Infinite loop that reads this atomic variable and performs the specified action on its value.

Link copied to clipboard
expect fun set(value: V)
actual fun set(value: V)
actual fun set(value: V)
Link copied to clipboard
inline fun <V> Atomic<V>.tryUpdate(function: (V) -> V): Boolean
Link copied to clipboard
inline fun <V> Atomic<V>.update(function: (V) -> V)
Link copied to clipboard
inline fun <V> Atomic<V>.updateAndGet(function: (V) -> V): V

Updates variable atomically using the specified function of its value and returns its new value.