withTVar

suspend fun <A, R> withTVar(initial: A, block: STM.(TVar<A>) -> R): R(source)

Creates a TVar with initial value and immediately uses it in a transaction.

Convenience function for cases where you need a short-lived transactional variable.

Example:

val result = withTVar(0) { tvar ->
tvar.write(tvar.read() + 42)
tvar.read()
} // 42

Return

the result of the transaction

Parameters

initial

the initial value for the TVar

block

the STM transaction block that receives the created TVar