Created
March 3, 2019 16:42
-
-
Save MaximBazarov/8f14c8c4372a63ed5d312f3614f1298b 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 | |
/// Gives your value thread protection | |
public final class AtomicValue<T> { | |
public var value: T { | |
get { | |
return lock.sync { | |
return self._value | |
} | |
} | |
set { | |
lock.async { | |
self._value = newValue | |
} | |
} | |
} | |
private let lock = DispatchQueue(label: "code.unicore.atomic-value-lock") | |
private var _value: T | |
public init(_ value: T) { | |
self._value = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you elaborate?