compareAndSet

suspend fun <A> TVar<A>.compareAndSet(expected: A, new: A): Boolean(source)

Atomically updates the TVar only if the current value matches expected.

Example:

val tvar = TVar.new("initial")
val swapped = tvar.compareAndSet("initial", "updated") // true
val failed = tvar.compareAndSet("initial", "other") // false (already "updated")

Return

true if the update occurred, false otherwise

Parameters

expected

the value to compare against

new

the value to set if the comparison succeeds