dispatch_semaphore_t信号量的使用

此处crash是因为sema被销毁的时候还在被使用,系统判断sema是否被使用的标志为sema被销毁时的值是否与初始化的值相同。我们可以看一下堆栈信息

dispatch_semaphore_t信号量的使用_第1张图片
sema导致的crash堆栈

我们可以看到这句话"BUG IN CLIENT OF LIBDISPATCH: Semaphore object deallocated while in use"。  sema被销毁时当前的信号量必须大于等于初始化的值。

1.sema的方法:

//dispatch_semaphore_create(value) 参数value必须大于等于0

dispatch_semaphore_t sema = dispatch_semaphore_create(max);

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);此方法会使信号量value-1,当value=0时,调用此方法会阻塞线程

dispatch_semaphore_signal(sema);此方法会发送一个信号量使value+1

2.sema的使用:

dispatch_semaphore_t信号量的使用_第2张图片

你可能感兴趣的:(dispatch_semaphore_t信号量的使用)