How to Implement Semaphores in iOS Application?

前端 未结 4 433
长发绾君心
长发绾君心 2021-02-06 10:52

Is it possible to implement Counting Semaphore in ios application?

4条回答
  •  爱一瞬间的悲伤
    2021-02-06 11:35

    Like this:

    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);
    

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

提交回复
热议问题