withValue(_:)

Perform an operation with isolated access to the underlying value.

LockIsolated.swift:41
func withValue<T>(_ operation: (inout Value) throws -> T) rethrows -> T where T : Sendable

Parameters

operation

An operation to be performed on the the underlying value with a lock.

Returns

The result of the operation.

Useful for modifying a value in a single transaction.

// Isolate an integer for concurrent read/write access:
var count = LockIsolated(0)

func increment() {
  // Safely increment it:
  self.count.withValue { $0 += 1 }
}