我对DispatchQueue和DispatchSemaphore感到困惑。如下例所示:
let semaphore : DispatchSemaphore = DispatchSemaphore(value:1)
for i in 1...40 {
DispatchQueue.global().async{
semaphore.wait()
NSLog("......1-%d",i)
semaphore.signal()
}
}
我认为它应该打印1 ... 40,实际上,它只打印约25,结果如下:
2016-11-18 19:05:38.786 MyPlayground[7436:495171] ......1-1
2016-11-18 19:05:38.787 MyPlayground[7436:495175] ......1-2
......
2016-11-18 19:05:38.797 MyPlayground[7436:495258] ......1-23
2016-11-18 19:05:38.797 MyPlayground[7436:495244] ......1-24
原因是什么?
答案 0 :(得分:3)
因为您正在运行异步,Playground会在所有40次迭代完成之前完成。将这两行添加到代码的开头或结尾:
error: patch failed: <filename>:<linenumber>
error: while searching for :
cout << "}" << endl; // example of a line in your patch