Exposes Objective-C's @synchronized directive to Swift
Swift Objective-C Ruby
Switch branches/tags
Clone or download
Latest commit 319c019 Oct 11, 2017
Failed to load latest commit information.
Synchronized.xcodeproj [Swift 4] Update the project settings for Xcode 9 and Swift 4 Oct 11, 2017
Synchronized [Swift 4] Update the project settings for Xcode 9 and Swift 4 Oct 11, 2017
.gitignore synchronized() functions for Swift along with tests Sep 16, 2014
LICENSE [CocoaPod] Add a podspec for 1.0.0 Jan 5, 2015
README.md [README] Update version numbers in README to 4.0 Oct 11, 2017
Synchronized.podspec [4.0.0] Bump podspec version to 4.0.0 for Swift 4 support Oct 11, 2017
circle.yml [Xcode 8] Update for Xcode 8 and Swift 3 Sep 8, 2016


Synchronized Circle CI Build Status

Version License Platform

Exposes Objective-C's @synchronized directive to Swift. Like the Objective-C directive, Synchronized acquires a mutex lock, runs some code, and releases the lock when the code completes or throws an exception.

Linking the Framework

Synchronized is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Synchronized", "~> 4.0"

You can also use Carthage to fetch it from GitHub:

github "ide/Synchronized" ~> 4.0

Once the framework is linked this Swift code should compile:

import Synchronized

let x = synchronized(NSObject()) { 0 }

Public API

public func synchronized(object: AnyObject, closure: () -> Void)


synchronized(mutexObject) {
  // Code to run in your critical section

public func synchronized<T>(object: AnyObject, closure: () -> T) -> T


let value = synchronized(threadUnsafeDictionary) {

Differences from @synchronized

Objective-C's @synchronized is a language-level directive and does not introduce a new function scope. This means that return statements cause the program to return from the surrounding function that contains the @synchronized directive.

- (void)returnDifferenceExample
    @synchronized {
    NSLog(@"This line of code does not run.");

In contrast, Synchronized uses closures which do introduce a function scope. Returning from a closure passed to synchronized exits only the closure, not the surrounding function.

func returnDifferenceExample() {
    synchronized {
    println("This line of code does run.")

Synchronized's closures are annotated with the @noclosure attribute, which removes the need to access instance variables with self., so it is similar to Objective-C's @synchronized directive in this regard.