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 self threadsafe.

    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 f to the wrapped instance.

    Declaration

    Swift

    public func read<R>(applying f: (SharedValue) throws -> R) rethrows -> R
  • Returns the result of thread-safely applying modification to the wrapped instance.

    Requires

    modification does not mutate any existing state.

    Warning

    Swift silently creates mutable captures in closures! If modification mutates 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