awesome-tips / iOS-Tips

iOS知识小集
MIT License
3.29k stars 451 forks source link

NSRecursiveLock在Playground里还是死锁 #8

Closed sunzeboy closed 6 years ago

sunzeboy commented 6 years ago

let lock = NSRecursiveLock() var recursiveMethod: ((Int) -> Void)! = nil

recursiveMethod = { value in

defer { lock.unlock() }

lock.lock()

guard value > 0 else { return }

print(value) sleep(2) recursiveMethod(value - 1) }

DispatchQueue.global().async {

print("start") recursiveMethod(5) print("end") }

southpeak commented 6 years ago

@sunzeboy ,这个不是死锁啊,是你的代码主线程执行完了,相当于程序退出了,所以二级线程也退出了~~你在代码最后加上个 while(true) {} 循环试试