Skip to content

Instantly share code, notes, and snippets.

@LK-Simon
Last active July 13, 2022 10:51
Show Gist options
  • Save LK-Simon/9da3d4238678f5c3a4702fc416c93f87 to your computer and use it in GitHub Desktop.
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
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()
}
}
@LK-Simon
Copy link
Author

Usage example is very simple:

@ThreadSafe var someInt: Int = 10

Any access to someInt for either Get or Set operations will be encapsulated within the safety of the DispatchSemaphore, ensuring that only one Thread is operating on the value at any given moment.

@LK-Simon
Copy link
Author

Access to the underlying Lock is also available:

@ThreadSafe var someIntArray: [Int] = []

// and inside some method
_someIntArray.lock.wait() // Acquire the lock
for value in someIntArray {
    print(value)
}
_someIntArray.lock.signal() // Release the lock

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.

@LK-Simon
Copy link
Author

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