compareAndSet

expect fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

This operation has so-called strong semantics, meaning that it returns false if and only if current and expected values are not equal.

Comparison of values is done by reference.

Since Kotlin

2.1

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val a = AtomicReference("aaa")
// Current value "aaa" is equal to the expected value "aaa" -> compareAndSet succeeds.
println(a.compareAndSet("aaa", "bbb")) // true
println(a.load()) // bbb

// Current value "bbb" is not equal to the expected value "aaa" -> compareAndSet fails.
println(a.compareAndSet("aaa", "kkk")) // false
println(a.load()) // bbb 
   //sampleEnd
}
actual fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Comparison of values is done by value.

Since Kotlin

2.1
actual fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Comparison of values is done by value.

Has the same memory effects as java.util.concurrent.atomic.AtomicReference.compareAndSet.

Since Kotlin

2.1
actual fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

This operation has so-called strong semantics, meaning that it returns false if and only if current and expected values are not equal.

Comparison of values is done by reference.

Since Kotlin

2.1
actual fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Comparison of values is done by value.

Since Kotlin

2.1
actual fun compareAndSet(expectedValue: T, newValue: T): Boolean(source)

Atomically stores the given new value into this AtomicReference if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Comparison of values is done by value.

Since Kotlin

2.1