ios加锁之信号量

dispatch_semaphore_create(long value); // 创建信号量dispatch_semaphore_signal(dispatch_semaphore_t deem); // 发送信号量 信号量会加1,释放资源,相当于解锁

dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout); // 等待信号量,如果信号量为0则等待。否则往下执行并且信号量减1.相当于加锁

标准用法例子

dispatch_semaphore_t _semaphore = dispatch_semaphore_create(1)

dispatch_semaphore_wait(_semaphore,DISPATCH_TIME_FOREVER)

需要加锁的代码

dispatch_semaphore_signal(_semaphore); 

你可能感兴趣的:(ios加锁之信号量)