gpt4 book ai didi

c++ - 信号量未初始化

转载 作者:行者123 更新时间:2023-11-28 05:41:25 24 4
gpt4 key购买 nike

我正在尝试使用 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,我不确定为什么它没有正确初始化。

最佳答案

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

关于c++ - 信号量未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37015277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com