Last active
August 5, 2019 01:26
-
-
Save dagronf/6a0e8cce9abf7ee38def0601dcabd387 to your computer and use it in GitHub Desktop.
Atomic class from https://www.objc.io/blog/2018/12/18/atomic-variables/
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
// FROM: https://www.objc.io/blog/2018/12/18/atomic-variables/ | |
final class Atomic<A> { | |
private let queue = DispatchQueue(label: "Atomic serial queue") | |
private var _value: A | |
init(_ value: A) { | |
self._value = value | |
} | |
var value: A { | |
get { | |
return queue.sync { self._value } | |
} | |
} | |
func mutate(_ transform: (inout A) -> ()) { | |
queue.sync { | |
transform(&self._value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use like :-