Atomic

open class Atomic<T>

Atomic provides thread-safety to access a variable.

  • The thread-safe variable.

    Declaration

    Swift

    open var value: T
  • Init the Atomic to access the variable in a thread-safe way.

    Declaration

    Swift

    public init(_ value: T)

    Parameters

    value

    The variable needs to be thread-safe.

  • The provides a scheme to access and change the underlying variable in a block.

    The variable can be accessed with Box<T>.value as:

    let atomic = Atomic([1,2,3])
    atomic.withBox { array in
       array.value.append(4)
       return array.value.reduce(0, combine: +)
    }
    

    Declaration

    Swift

    open func withBox<U>(_ block: (Box<T>) -> (U)) -> U

    Parameters

    block

    The code to run with the variable wrapped in a Box.

    Return Value

    Any value returned by the block.