Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Wraps a value with a DispatchSemaphore(value: 1) to prevent overlapping access.
- @propertyWrapper public struct Semaphored<T> {
- private var value: T
- private var semaphore = DispatchSemaphore(value: 1)
- public var wrappedValue: T {
- get {
- defer { semaphore.signal() }
- semaphore.wait()
- return value
- }
- set {
- semaphore.wait()
- value = newValue
- semaphore.signal()
- }
- }
- public init(wrappedValue: T) {
- self.value = wrappedValue
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement