updateAndFetchAt

expect inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when an integer value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

expect inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a long value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

expect inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a reference at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when an integer value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a long value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a reference at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when an integer value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a long value at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

transform may be invoked more than once to recompute a result. That may happen, for example, when a reference at the specified index 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

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun AtomicIntArray.updateAndFetchAt(index: Int, transform: (Int) -> Int): Int(source)

Atomically updates the element of this AtomicIntArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicIntArray(intArrayOf(1, 2, 3))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10 }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun AtomicLongArray.updateAndFetchAt(index: Int, transform: (Long) -> Long): Long(source)

Atomically updates the element of this AtomicLongArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicLongArray(longArrayOf(1L, 2L, 3L))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue * 10L }
println(updatedValue) // 20
println(a.toString()) // [1, 20, 3]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}

actual inline fun <T> AtomicArray<T>.updateAndFetchAt(index: Int, transform: (T) -> T): T(source)

Atomically updates the element of this AtomicArray at the given index using the transform function and returns the updated value of the element.

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

Since Kotlin

2.2

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("hello", "concurrent", "world"))
val updatedValue = a.updateAndFetchAt(1) { currentValue -> currentValue.uppercase() }
println(updatedValue) // CONCURRENT
println(a.toString()) // [hello, CONCURRENT, world]

// The index is out of array bounds
// a.updateAndFetchAt(10) { it } // will fail with IndexOutOfBoundsException 
   //sampleEnd
}