update

expect inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic integer value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

On platforms that do no support multi-threading (JS and Wasm), this operation has a trivial implementation and transform will be invoked exactly once.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

expect inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic long value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

On platforms that do no support multi-threading (JS and Wasm), this operation has a trivial implementation and transform will be invoked exactly once.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

expect inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic reference was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

On platforms that do no support multi-threading (JS and Wasm), this operation has a trivial implementation and transform will be invoked exactly once.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}
actual inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

JS does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

JS does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

JS does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}
actual inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic integer value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic long value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic reference was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}
inline fun AtomicNativePtr.update(transform: (NativePtr) -> NativePtr)(source)

Atomically updates the value of this AtomicNativePtr with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this pointer was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

actual inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic integer value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic long value was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

transform may be invoked more than once to recompute a result. That may happen, for example, when this atomic reference was concurrently updated while transform was applied, or due to a spurious compare-and-set failure. The latter is implementation-specific, and it should not be relied upon.

It's recommended to keep transform fast and free of side effects.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}
actual inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}
actual inline fun AtomicInt.update(transform: (Int) -> Int)(source)

Atomically updates the value of this AtomicInt with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicInt(7)
a.update { currentValue ->
    when (currentValue % 2) {
        0 -> currentValue / 2
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun AtomicLong.update(transform: (Long) -> Long)(source)

Atomically updates the value of this AtomicLong with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   val a = AtomicLong(7L)
a.update { currentValue ->
    when (currentValue % 2) {
        0L -> currentValue / 2L
        else -> 3 * currentValue + 1
    }
}
println(a.load()) // 22 
   //sampleEnd
}

actual inline fun <T> AtomicReference<T>.update(transform: (T) -> T)(source)

Atomically updates the value of this AtomicReference with the value obtained by calling the transform function on the current value.

Wasm does not support multithreading, thus the implementation is trivial, and transform will only be invoked exactly once to compute the result.

Since Kotlin

2.2

Samples

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

fun main() { 
   //sampleStart 
   data class Wallet(val owner: String, val balance: Long)

val a = AtomicReference(Wallet("Kodee", 100_00L))
a.update { wallet ->
    wallet.copy(balance = wallet.balance + 1_00) // Kodee got a buck!
}
println(a.load()) // Wallet(owner=Kodee, balance=10100) 
   //sampleEnd
}