我查阅了Swift书籍,但未能找到@synchronized的Swift版本。在Swift中如何实现互斥?
dispatch_barrier_async是更好的方式,而不会阻塞当前线程。
dispatch_barrier_async(accessQueue, { dictionary[object.ID] = object })
基于ɲeuroburɳ,测试一个子类案例
class Foo: NSObject {
func test() {
print("1")
objc_sync_enter(self)
defer {
objc_sync_exit(self)
print("3")
}
print("2")
}
}
class Foo2: Foo {
override func test() {
super.test()
print("11")
objc_sync_enter(self)
defer {
print("33")
objc_sync_exit(self)
}
print("22")
}
}
let test = Foo2()
test.test()
1
2
3
11
22
33
class Lockable {
let lockableQ:dispatch_queue_t
init() {
lockableQ = dispatch_queue_create("com.blah.blah.\(self.dynamicType)", DISPATCH_QUEUE_SERIAL)
}
func lock(closure: () -> ()) {
dispatch_sync(lockableQ, closure)
}
}
class Foo: Lockable {
func boo() {
lock {
....... do something
}
}
removeFirst()
创建包装器时,该怎么办? - ScottyBlades