A readers-writer lock is a shared-exclusive lock. It allows for concurrent read locks, but require exclusive access to write locks.
To run the example project, clone the repo, and run pod install
from the Example directory first.
ReadersWriterLock is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ReadersWriterLock'
// import the module in your source
import ReadersWriterLock
// Create a normal dictionary
var dictionary = [String: String]()
// Create the lock
let lock = ReadersWriterLock()
// Write values to the dictionary using the write lock
lock.withWriteLock {
dictionary["value"] = "test"
}
// Read values from the dictionary using a read lock
let value = lock.withReadLock {
dictionary["value"]!
}
print(value) // "test"
ReadersWriterLock is available under the MIT license. See the LICENSE file for more info.