Swiftpack.co -  grype/RWLock-Swift as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
grype/RWLock-Swift
Read-Write lock in Swift
.package(url: "https://github.com/grype/RWLock-Swift.git", from: "1.0.0")

RWLock

Swift wrapper around pthread_rwlock.

See Read-Write Lock for details.

Usage:

let lock = RWLock()
lock.readProtected {
    // do something concurrently with other reads and until write lock is acquired 
}
lock.writeProtected {
    // do something that requires exclusive access
}

Using propety wrapper:

class Foo {
    @RWLocked var items = [Any](https://raw.github.com/grype/RWLock-Swift/main/)
    
    func add(_ item: Any) {
        items.append(item)
    }
    
    func removeAll() {
        items.removeAll()
    }
    
    func iterate() {
        items.forEach { 
            // do something with each item
        }
    }
    
}

Any modifications to items array will require write protection - this applies to methods add(_:) and removeAll(). In contrast, iterate()ing over the array requires read access. This means that you won't be able to add(_:) or removeAll() until iteration is complete, and you won't be able to iterate() until add(_:) or removeAll() had finished.

GitHub

link
Stars: 0
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API