compareAndSet

abstract fun compareAndSet(expect: T, update: T): Boolean

Atomically compares the current value with expect and sets it to update if it is equal to expect. The result is true if the value was set to update and false otherwise.

This function use a regular comparison using Any.equals. If both expect and update are equal to the current value, this function returns true, but it does not actually change the reference that is stored in the value.

This method is thread-safe and can be safely invoked from concurrent coroutines without external synchronization.

Sources

common source
Link copied to clipboard