Created
September 16, 2020 08:38
-
-
Save jaclync/2ba5da6c5dfbf2ca873c34895ed8c55b to your computer and use it in GitHub Desktop.
Thread-safe property wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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