Atomic
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)
}
Content copied to clipboard
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!