Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.
🔒 Thread-Safe - Automatic synchronization for concurrent access
⚡ Simple API - Just add @Atomic to any property
🎯 Type-Safe - Works with any Swift type
📱 Cross-Platform - Works on iOS, macOS, tvOS, watchOS, and visionOS
⚡ Lightweight - Minimal footprint with zero dependencies
🧪 Well Tested - Comprehensive test coverage
| Platform | Minimum Version |
|---|---|
| iOS | 13.0+ |
| macOS | 10.15+ |
| tvOS | 13.0+ |
| watchOS | 6.0+ |
| visionOS | 1.0+ |
| Xcode | 15.3+ |
| Swift | 5.10+ |
Add the following dependency to your Package.swift:
dependencies:[.package(url:"https://github.com/space-code/atomic.git", from:"1.1.1")]Or add it through Xcode:
- File > Add Package Dependencies
- Enter package URL:
https://github.com/space-code/atomic.git - Select version requirements
import Atomic
@Atomicvarcounter=0
// Thread-safe increment from multiple threads
DispatchQueue.concurrentPerform(iterations:1000){ _ in
counter +=1}print("Final count: \(counter)") // Always 1000Simply add the @Atomic property wrapper to any property that needs thread-safe access:
import Atomic
classUserSession{@Atomicvaruser:User?@AtomicvarloginAttempts=0func login(username:String, password:String)asyncthrows{
// Thread-safe write
_loginAttempts.write{ $0 +=1}
// Perform authentication
letauthenticatedUser=tryawaitauthenticate(username, password)
// Thread-safe write with new value
_user.write(authenticatedUser)}func getCurrentUsername()->String?{
// Thread-safe read
_user.read{ $0?.username }}}import Atomic
classDataCache{@Atomicprivatevarcache:[String:Data]=[:]func store(_ data:Data, forKey key:String){
_cache.write{ cache incache[key]= data
}}func retrieve(forKey key:String)->Data?{
_cache.read{$0[key]}}func removeExpired(before date:Date){
_cache.write{ cache in
cache = cache.filter{ $0.value.timestamp > date }}}}- 🐛 Found a bug?Open an issue
- 💡 Have a feature request?Open an issue
- ❓ Questions?Start a discussion
- 🔒 Security issue? Email nv3212@gmail.com
We love contributions! Please feel free to help out with this project. If you see something that could be made better or want a new feature, open up an issue or send a Pull Request.
Bootstrap the development environment:
mise installNikita Vasilev
- Email: nv3212@gmail.com
- GitHub: @ns-vasilev
Atomic is released under the MIT license. See LICENSE for details.
Made with ❤️ by space-code