AtomicValue
public final class AtomicValue<Value> : @unchecked Sendable
A class that wraps access to its underlying value with an NSLocking instance.
-
Declaration
Swift
public init(initialValue: Value) -
Declaration
Swift
public func get() -> Value -
Declaration
Swift
public func set(_ newValue: Value) -
Sets AtomicValue to
newValueand returns the old valueDeclaration
Swift
public func getAndSet(_ newValue: Value) -> Value -
Performs
blockwith the current value, preventing other access until the block exits.Declaration
Swift
public func atomicallyPerform(block: (Value) -> Void) -
Performs
blockwith aninoutvalue, preventing other access until the block exits, and enabling the block to mutate the valueWarning
The AtomicValue lock is not reentrant. Specifically, it is not possible to call outside the block togetan AtomicValue (e.g., via a convenience property) while inside thewithblock. Attempting to do so will cause a deadlock.Declaration
Swift
public func with(block: (inout Value) -> Void)
-
Toggles the boolean’s value, and returns the old value.
Example:
let atomicBool = AtomicValue(initialValue: true) print(atomicBool.getAndToggle()) // prints "true" print(atomicBool.get()) // prints "false"Declaration
Swift
func getAndToggle() -> Value
-
Increments the current value by
amountand returns the incremented valueDeclaration
Swift
func increment(by amount: Value = 1) -> Value -
Decrements the current value by
amountand returns the decremented valueDeclaration
Swift
func decrement(by amount: Value = 1) -> Value
-
Declaration
Swift
func append(_ newElement: Value.Element) -
Declaration
Swift
func append(contentsOf sequence: some Sequence<Value.Element>) -
Declaration
Swift
func removeFirst() -> Value.Element -
Declaration
Swift
subscript(key: Value.Index) -> Value.Element { get }
View on GitHub