LoginSignup
38
39

More than 5 years have passed since last update.

Swift マルチスレッドでの同期処理(synchronized)

Last updated at Posted at 2015-06-30

Swiftでの同期処理

What is the Swift equivalent to Objective-C's “@synchronized”?

GCD を使う

let lockQueue = DispatchQueue(label: "com.test.LockQueue")
lockQueue.sync {
    // critical section code
}

objc_sync_enter / objc_sync_exit を使う

public func synchronized(obj: AnyObject, closure: () -> Void) {
    objc_sync_enter(obj)
    closure()
    objc_sync_exit(obj)
}

synchronized(self) {
    // critical section code
}

私は後者を採用しました。

38
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
39