如何在iOS应用程序中实现信号量?

Mus*_*P P 5 semaphore objective-c ios

是否可以在ios应用程序中实现Counting Semaphore?

djr*_*ero 14

对的,这是可能的.有很多可用的同步工具:

  • @Synchronized
  • NSLock
  • NSCondition
  • NSConditionLock
  • GCD信号量
  • pthread锁
  • ...

我建议阅读" 线程编程指南 "并询问更具体的内容.


Ada*_*ite 7

像这样:

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);
Run Code Online (Sandbox Code Playgroud)

信用http://www.g8production.com/post/76942348764/wait-for-blocks-execution-using-a-dispatch