Created
November 30, 2016 11:05
-
-
Save dreymonde/ee751a4605fd110478620002985e4100 to your computer and use it in GitHub Desktop.
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
import Foundation | |
public struct Synchronized<Value> { | |
fileprivate let accessQueue: DispatchQueue | |
fileprivate var value: Value | |
public init(_ value: Value, queueLabel: String = "\(Value.self)SynchronizedQueue") { | |
self.value = value | |
self.accessQueue = DispatchQueue(label: queueLabel) | |
} | |
public func get() -> Value { | |
return accessQueue.sync(execute: { return value }) | |
} | |
public mutating func set(_ value: Value) { | |
accessQueue.sync { | |
self.value = value | |
} | |
} | |
public mutating func set(_ change: (inout Value) -> ()) { | |
accessQueue.sync { | |
change(&value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment