为什么我无法在C代码中锁定信号量?

时间:2014-04-15 20:24:33

标签: c multithreading mutex deadlock semaphore

据我所知,下面的代码会导致死锁,而不会打印出来#34; hello world"。但是,当我使用gcc在我的计算机上编译(Macbook Air 2013年末,10.9.2)时,代码意外地打印了#34; hello world"并完成执行。

为什么以下代码不会导致死锁?

#include <stdio.h>
#include <semaphore.h>
int main() {
  sem_t prod_slots;
  sem_init(&prod_slots, 0, 0);
  sem_wait(&prod_slots);
  sem_wait(&prod_slots);
  sem_wait(&prod_slots);
  printf("%s\n", "hello world");
  return 1;
}

0 个答案:

没有答案