Skip to content

Instantly share code, notes, and snippets.

@jaclync
Created September 16, 2020 08:38
Show Gist options
  • Save jaclync/2ba5da6c5dfbf2ca873c34895ed8c55b to your computer and use it in GitHub Desktop.
Save jaclync/2ba5da6c5dfbf2ca873c34895ed8c55b to your computer and use it in GitHub Desktop.
Thread-safe property wrapper
@propertyWrapper
public struct ThreadSafe<Value> {
private let queue = DispatchQueue(label: "com.private")
private var value: Value
public init(wrappedValue: Value) {
self.value = wrappedValue
}
public var wrappedValue: Value {
get {
queue.sync { value }
}
set {
queue.sync {
value = newValue
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment