在iOS应用程序中是否可以实现计数信号量?
是的,这是可能的。
有许多同步工具可供使用:
我建议阅读《多线程编程指南》并询问更为具体的问题。
像这样:
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self methodWithABlock:^(id result){
//put code here
dispatch_semaphore_signal(sem);
[self methodWithABlock:^(id result){
//put code here
dispatch_semaphore_signal(sem);
}];
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
信用 http://www.g8production.com/post/76942348764/wait-for-blocks-execution-using-a-dispatch
我找不到原生的IOS对象来完成这个任务,但是使用C库完全可以:
#import "dispatch/semaphore.h"
...
dispatch_semaphore_t activity;
...
activity = dispatch_semaphore_create(0);
...
dispatch_semaphore_signal(activity);
...
dispatch_semaphore_wait(activity, DISPATCH_TIME_FOREVER);
DispatchSemaphore
。// initialization
let semaphore = DispatchSemaphore(value: initialValue)
// wait, decrement the semaphore count (if possible) or wait until count>0
semaphore.wait()
// release, increment the semaphore count
semaphore.signal()