信号量未初始化

时间:2016-05-03 22:12:16

标签: c++ pthreads semaphore

我正在尝试使用sem_open将我的信号量初始化为1,但我似乎无法让它工作。

 sem_t *Montague, *Capulet; // the two semaphores
 char sem_Montague[]= "jud_Montague"; // their names
 char sem_Capulet[]= "jud_Capulet"; // their names
 int semvalue;  // stores value returned by sem_getvalue()

 Montague = sem_open(sem_Montague,  O_CREAT, 0600, 1);
 // We specify that the semaphore should be created it it did
 // not exist already, prevent other users from accessing it
 // (0600) and set its initial value to one 

 if (Montague == SEM_FAILED) {
      perror("unable to create Montague semaphore");
      sem_unlink(sem_Montague);
      exit(1);
 } // if
 // Just to show that we can test the value of a semaphore

 sem_getvalue(Montague, &semvalue);
 printf("The initial value of sem_Montague is %d\n", semvalue);

当我运行代码时,输​​出为:

  The initial value of sem_Montague is 0

它应该是1,我不确定为什么它没有正确初始化。

1 个答案:

答案 0 :(得分:1)

我不相信为OS X实现了sem_getvalue。我认为它只是一个存根。