Contents

shareup/synchronized

Synchronized provides a simple Swift-y implementation of a lock and a recursive lock. `Lock` wraps `os_unfair_lock`. `RecursiveLock` wraps `pthread_mutex_t`. Synchronized's API is designed for convenience and simplicity.

@synchonized replacement

Synchronized started life as a simple (and simplified) re-implmentation of Objective-C's @synchronized for Swift. If you're interested in that version, you should look at v1.2.1.

Usage

let lock = Lock()
let lockInput: Int = 0
let lockOutput = lock.locked { lockInput + 1 }
XCTAssertEqual(1, lockOutput)

let recursiveLock = RecursiveLock()
let recursiveLockInput: Int = 0
let recursiveLockOutput = recursiveLock.locked { recursiveLockInput + 1 }
XCTAssertEqual(1, recursiveLockOutput)

let lockedInput = Locked(0)
lockedInput.access { $0 += 1 }
XCTAssertEqual(1, lockedInput.access { $0 })

Installation

Swift Package Manager

To use Synchronized with the Swift Package Manager, add a dependency to your Package.swift file:

let package = Package(
  dependencies: [
    .package(url: "https://github.com/shareup/synchronized.git", .upToNextMajor(from: "4.0.0"))
  ]
)

Linux is not currently supported

License

MIT

Package Metadata

Repository: shareup/synchronized

Default branch: main

README: README.md