Concurrent programming is never easy. When we talk about multithread or concurrent programming, it is not possible to avoid discussion on lock. To access a shared resource from different threads, we need to make sure the order of them (either reading or writing) is serial, to avoid unexpected behaviors. In Cocoa and Objective-C, there are some ways to add lock. The most common one would be @synchronized, which could behave as a modifier of a variable. It will add a mutual exclusion when the variable is accessed, and remove it when over. So, the value will not be modified by another thread. Let's say we have a method which accepts an argument. If we want this method thread-safe, we could add a lock to the argument to make sure it will not be changed in the scope:

-(void) myMethod:(id)anObj {
    @synchronized(anObj) {
        // anObj will not be changed by other threads.
    }
}

Locking and unlocking are not free, it will consume our performance. Keep things as simple as possible is the golden rule in multithread programming design. Back to @synchronized, although it is very straightforward to use, in Swift, it does not exist there. Under the hood, @synchronized calls objc_sync_enter and objc_sync_exit function of objc_sync, and does some exception checking. Therefore, if we want a similar feature in Swift, we can do like this:

func myMethod(anObj: AnyObject!) {
    objc_sync_enter(anObj)

    // anObj will not be changed by other threads between enter and exit.

    objc_sync_exit(anObj)
}

Furthermore, if you think it is boilerplate code to write this, you can wrap objc_sync_enter and objc_sync_exit into a global function and receive a closure:

func synchronized(lock: AnyObject, closure: () -> ()) {
    objc_sync_enter(lock)
    closure()
    objc_sync_exit(lock)
}

Combined with the trailing closures feature in Swift, you can use the new synchronized in a similar syntax as you did in Objective-C:

func myMethod(anObj: AnyObject!) {
    synchronized(anObj) {
        // anObj will not be changed by other threads.
    }
}