Created
December 4, 2018 14:21
-
-
Save adiki/6ab2b3b72400a2c5c787d9c90ea20f05 to your computer and use it in GitHub Desktop.
This file contains 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
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