Why does this code cause “EXC_BAD_INSTRUCTION”?

后端 未结 3 1508
悲哀的现实
悲哀的现实 2020-12-23 22:57
dispatch_semaphore_t aSemaphore = dispatch_semaphore_create(1);        
dispatch_semaphore_wait(aSemaphore, DISPATCH_TIME_FOREVER);
dispatch_release(aSemaphore);
         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 23:22

    Somewhat more succinct answer: You are creating the semaphore with the wrong value, it should be zero. Creating it with a value of 1 means you are later releasing a semaphore that's still "in use" and GCD is deliberately generating an illegal instruction in order to help you debug the fact that you have a semaphore with more waiters on it.

提交回复
热议问题