【问题标题】:Semaphore not initializing信号量未初始化
【发布时间】:2016-05-03 22:12:16
【问题描述】:

我正在尝试使用 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...你使用什么编译器/工具链?什么平台?
  • 我正在使用带有 g++ 编译器的 OS Yosemite。使用 g++ 编译 -ipthread verona.cpp -o verona
  • Mac 的有趣之处在于,g++ 经常默默地别名为 clang。

标签: c++ pthreads semaphore


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多