Skip to content

Instantly share code, notes, and snippets.

@dreymonde
Created November 30, 2016 11:05
Show Gist options
  • Save dreymonde/ee751a4605fd110478620002985e4100 to your computer and use it in GitHub Desktop.
Save dreymonde/ee751a4605fd110478620002985e4100 to your computer and use it in GitHub Desktop.
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