Atomic
@propertyWrapper
public class Atomic<Value>
Atomic reads and writes via a property wrapper
Note:
When performing simultaneous read/write operations like +=
you must use the mutate
function for atomic functionality
@Atomic public var balance: Decimal
// Write without read works atomically as expected
func resetBalance() { balance = 1000.00 }
// This does **NOT** guarantee atomic operations
func incrementBalane() { balance += 500 }
// This does guarantee atomic operations
func decrement() { $balance.mutate { $0 -= 500 } }
-
Undocumented
Declaration
Swift
public init(wrappedValue: Value)
-
The value of the atomic wrapped object.
Declaration
Swift
public var wrappedValue: Value { get set }
-
Atomic mutation
Atomic’s
wrappedValue
is capable of atomic gets and sets, however when attempting to do both with operators like+=
that is a nonatomic operation. It is for this reason that the mutate function existsDeclaration
Swift
public func mutate(_ mutation: (inout Value) -> Void)
Parameters
mutation
A mutating function used when getting and setting the wrapped value is necessary