<https://kotlinlang.org/docs/shared-mutable-state-...
# announcements
n
https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#volatiles-are-of-no-help It says here that volatiles do not provide atomicity of larger actions like increments. Increments is the smallest action i can think of. Does that mean volatiles has zero value? is this only in the case of coroutines or also in the case of threads?
d
Postfix increment is a sugar for three operations:
Copy code
x++
// same as
val tmp = x // read
x = x.inc() // read and write
tmp
So anything can happens between those reads and writes.
Since
volatile
is JVM concept I recommend you to read about Java Memory Model (JMM) https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.2
🙏 1
c
Yeah. I highly recommend you avoid using volatile unless you know JMM inside and out. Often times it’s better to reach for atomicity abstractions because the performance differences are tiny and you have to consider other people will need to maintain your code.
e
with Atomic*FieldUpdater volatile can have many similar operations as Atomic* wrapper types. but it'sa bit awkward to use. kotlinx.atomicfu helps