Last active
January 10, 2019 19:18
-
-
Save jakebromberg/c3cbc1fc02c92930a1b0976c588f3971 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 | |
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