Last active
July 13, 2022 10:51
-
-
Save LK-Simon/9da3d4238678f5c3a4702fc416c93f87 to your computer and use it in GitHub Desktop.
Swift Property Wrapper to make a Var Thread-Safe using an enforced Semaphore Lock
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
import Foundation | |
@propertyWrapper | |
public struct ThreadSafe<T> { | |
public var lock = DispatchSemaphore(value: 1) | |
private var value: T | |
public var wrappedValue: T { | |
get { | |
lock.wait() | |
let result = value | |
lock.signal() | |
return result | |
} | |
set { | |
lock.wait() | |
value = newValue | |
lock.signal() | |
} | |
} | |
public init(wrappedValue: T) { | |
lock.wait() | |
value = wrappedValue | |
lock.signal() | |
} | |
} |
OBSOLITE!
Use instead my Swift Package, which you can find with full instructions here: https://github.com/Flowduino/ThreadSafeSwift/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Access to the underlying Lock is also available:
This way, you can marshal the lock wherever you need if a singular get/set operation is not what you're looking to achieve.
This example demonstrates with an Array, simply because you would want to retain the Lock for the duration of any Iterative Operation. The same can be used for any Collection type, as well as against any Structured Object (struct or class) where you're setting/getting multiple member values etc.