{"id":32147267,"url":"https://github.com/somerandomiosdev/readwritelock","last_synced_at":"2025-10-21T08:58:08.242Z","repository":{"id":62452620,"uuid":"190574359","full_name":"SomeRandomiOSDev/ReadWriteLock","owner":"SomeRandomiOSDev","description":"A Swifty Read/Write lock for iOS, macOS, tvOS, and watchOS","archived":false,"fork":false,"pushed_at":"2021-10-25T16:36:30.000Z","size":45,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-21T08:57:34.539Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SomeRandomiOSDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-06T12:03:41.000Z","updated_at":"2024-12-03T12:40:15.000Z","dependencies_parsed_at":"2022-11-01T23:46:14.282Z","dependency_job_id":null,"html_url":"https://github.com/SomeRandomiOSDev/ReadWriteLock","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/SomeRandomiOSDev/ReadWriteLock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SomeRandomiOSDev%2FReadWriteLock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SomeRandomiOSDev%2FReadWriteLock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SomeRandomiOSDev%2FReadWriteLock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SomeRandomiOSDev%2FReadWriteLock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SomeRandomiOSDev","download_url":"https://codeload.github.com/SomeRandomiOSDev/ReadWriteLock/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SomeRandomiOSDev%2FReadWriteLock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280232861,"owners_count":26295149,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2025-10-21T08:58:07.010Z","updated_at":"2025-10-21T08:58:08.236Z","avatar_url":"https://github.com/SomeRandomiOSDev.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ReadWriteLock\n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d30d31c29f17449481b97a04610ff5b9)](https://app.codacy.com/app/SomeRandomiOSDev/ReadWriteLock?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=SomeRandomiOSDev/ReadWriteLock\u0026utm_campaign=Badge_Grade_Dashboard)\n[![License MIT](https://img.shields.io/cocoapods/l/ReadWriteLock.svg)](https://cocoapods.org/pods/ReadWriteLock)\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/ReadWriteLock.svg)](https://cocoapods.org/pods/ReadWriteLock) \n[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) \n[![Platform](https://img.shields.io/cocoapods/p/ReadWriteLock.svg)](https://cocoapods.org/pods/ReadWriteLock)\n[![Code Coverage](https://codecov.io/gh/SomeRandomiOSDev/ReadWriteLock/branch/master/graph/badge.svg)](https://codecov.io/gh/SomeRandomiOSDev/ReadWriteLock)\n\n![Swift Package](https://github.com/SomeRandomiOSDev/ReadWriteLock/workflows/Swift%20Package/badge.svg)\n![Xcode Project](https://github.com/SomeRandomiOSDev/ReadWriteLock/workflows/Xcode%20Project/badge.svg)\n![Cocoapods](https://github.com/SomeRandomiOSDev/ReadWriteLock/workflows/Cocoapods/badge.svg)\n![Carthage](https://github.com/SomeRandomiOSDev/ReadWriteLock/workflows/Carthage/badge.svg)\n\n**ReadWriteLock** is a lightweight framework of a safe an easy implementation of a read-write lock for iOS, macOS, tvOS, and watchOS.\n\n## Installation\n\n**ReadWriteLock** is available through [CocoaPods](https://cocoapods.org), [Carthage](https://github.com/Carthage/Carthage) and the [Swift Package Manager](https://swift.org/package-manager/). \n\nTo install via CocoaPods, simply add the following line to your Podfile:\n\n```ruby\npod 'ReadWriteLock'\n```\n\nTo install via Carthage, simply add the following line to your Cartfile:\n\n```ruby\ngithub \"SomeRandomiOSDev/ReadWriteLock\"\n```\n\nTo install via the Swift Package Manager add the following line to your `Package.swift` file's `dependencies`:\n\n```swift\n.package(url: \"https://github.com/SomeRandomiOSDev/ReadWriteLock.git\", from: \"1.0.0\")\n```\n\n## Usage\n\nFirst import **ReadWriteLock** at the top of your Swift file:\n\n```swift\nimport ReadWriteLock\n```\n\nAfter importing simply instantiate a `ReadWriteLock` instance and start acquiring locks:\n\n```swift\nlet lock = ReadWriteLock()\nvar protectedResource = ...\n\nDispatchQueue.global(qos: .background).async {\n    ...\n\n    lock.acquireWriteLock {\n        protectedResource = ...\n    }\n}\n\n...\n\nDispatchQueue.global(qos: .background).async {\n    ...\n    \n    lock.acquireReadLock {\n        process(protectedResource)\n    }\n}\n```\n\nOr you can conditionally attempt to acquire a lock. Acquiring a lock in this way will not block if the lock has already been claimed:\n\n```swift\n...\n\nlock.attemptAcquireReadLock { isLockAcquired in\n    if isLockAcquired {\n        process(protectedResource)\n    } else {\n        // `lock` is currently locked for writing. \n    }\n}\n```\n\nAcquiring locks uses Swift closures to scope the lifetime of the read/write lock. This prevents lingering locks when the `ReadWriteLock` object is deallocated and helps prevent deadlocks. Note that if you attempt to acquire a write lock from inside of the scope of an already acquired read or write lock, or acquiring a read lock from inside of the scope of an already acquired write lock, the thread will deadlock:\n\n```swift \n// All of these code blocks will deadlock\n\nlock.acquireWriteLock {\n    ...\n    \n    lock.acquireWriteLock {\n        // deadlock\n    }\n}\n\nlock.acquireReadLock {\n    ...\n\n    lock.acquireWriteLock {\n        // deadlock\n    }\n}\n\nlock.attemptAcquireWriteLock { isLockAcquired in\n    ...\n\n    if isLockAcquired {\n        lock.acquireWriteLock {\n            // deadlock\n        }\n    }\n}\n\n...\n```\n\nThe benefit of read/write locks over traditional locks is that you may have (virtually) unlimited read locks, even nested read locks, without deadlocking:\n\n```swift\nlock.acquireReadLock {\n    ...\n    \n    lock.acquireReadLock {\n        // won't deadlock\n    }\n    \n    lock.attemptAcquireReadLock { isLockAcquired in \n        // isLockAcquired is `true`\n    }\n}\n```\n\nRead/write locks are ideal for synchronizing access to properties:\n\n```swift\nstruct SynchronizedValue\u003cT\u003e {\n    private var lock = ReadWriteLock()\n    private var _value: T\n    \n    var value: T {\n        get {\n            return lock.acquireReadLock { _value }\n        }\n        set {\n            lock.acquireWriteLock { _value = newValue }\n        }\n    }\n}\n```\n\n## Contributing\n\nIf you have need for a specific feature or you encounter a bug, please open an issue. If you extend the functionality of **ReadWriteLock** yourself or you feel like fixing a bug yourself, please submit a pull request.\n\n## Author\n\nJoe Newton, somerandomiosdev@gmail.com\n\n## License\n\n**ReadWriteLock** is available under the MIT license. See the `LICENSE` file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomerandomiosdev%2Freadwritelock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsomerandomiosdev%2Freadwritelock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomerandomiosdev%2Freadwritelock/lists"}