SharedMutable
public final class SharedMutable<SharedValue> : @unchecked Sendable where SharedValue : Sendable
A threadsafe shared mutable wrapper for a SharedValue instance.
Warning
The shared value has reference semantics; it’s up to the programmer to ensure that the value only used monotonically.-
The synchronization mechanism that makes
selfthreadsafe.Declaration
Swift
private let mutex: DispatchQueue -
The (thread-unsafe) stored instance.
Declaration
Swift
private var storage: SharedValue -
Creates an instance storing
toBeShared.Declaration
Swift
public init(_ toBeShared: SharedValue) -
Returns the result of thread-safely applying
fto the wrapped instance.Declaration
Swift
public func read<R>(applying f: (SharedValue) throws -> R) rethrows -> R -
Returns the result of thread-safely applying
modificationto the wrapped instance.Requires
modificationdoes not mutate any existing state.Warning
Swift silently creates mutable captures in closures! Ifmodificationmutates anything other than its local variables, you can create data races and undefined behavior.Declaration
Swift
public func modify<R>(applying modification: (inout SharedValue) throws -> R) rethrows -> R
View on GitHub