Skip to content

Instantly share code, notes, and snippets.

@adiki
Created December 4, 2018 14:21
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 adiki/6ab2b3b72400a2c5c787d9c90ea20f05 to your computer and use it in GitHub Desktop.
Save adiki/6ab2b3b72400a2c5c787d9c90ea20f05 to your computer and use it in GitHub Desktop.
final class ThreadSafe<T> {
private var _value: T
private let queue = DispatchQueue(label: ThreadSafe.description())
init(_ value: T) {
self._value = value
}
var value: T {
return queue.sync { _value }
}
func atomically(_ transform: (inout T) -> ()) {
queue.sync {
transform(&self._value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment