Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
Last active January 10, 2019 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakebromberg/c3cbc1fc02c92930a1b0976c588f3971 to your computer and use it in GitHub Desktop.
Save jakebromberg/c3cbc1fc02c92930a1b0976c588f3971 to your computer and use it in GitHub Desktop.
import Foundation
final class Threadsafe<Value> {
init(_ value: Value) {
self._value = value
}
var value: Value {
return queue.sync { return _value }
}
private var _value: Value
private let queue = DispatchQueue(label: "threadsafe")
func mutate(_ work: @escaping (inout Value) -> ()) {
queue.sync {
work(&self._value)
}
}
}
var threadsafeValue = Threadsafe(3)
threadsafeValue.mutate { value in
value += 1
}
print(threadsafeValue.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment