简体   繁体   English

C ++ Macs OS X semaphore.h:出现“ sem_open()”和“ sem_wait()”问题

[英]C++ Macs OS X semaphore.h: trouble with `sem_open()` and `sem_wait()`

#include <semaphore.h>

sem_t* x;

int main () 
{
    x = sem_open("x", O_CREAT, 0, 0);;
    sem_wait(x); sem_wait(x); sem_wait(x);
    std::cout << "\ndone\n";
}

This code shouldn't even pass the first sem_wait() but on my system it reaches the end of main() . 该代码甚至不应该通过第一个sem_wait()但是在我的系统上,它将到达main()的末尾。 Everything I have read, such as here and here , say that, although Mac OS X does not support sem_init() , it does support sem_open() . 我读过的所有内容(例如此处此处 )都表示,尽管Mac OS X不支持sem_init() ,但它确实支持sem_open() However, using sem_open() as above hasn't fixed the problem. 但是,如上所述使用sem_open()不能解决问题。 I'm running OS X 10.5.7. 我正在运行OS X 10.5.7。

Try putting sem_unlink("x"); 尝试放入sem_unlink("x"); before sem_open() , I'm sure it's not your first attempt on it. sem_open()之前,我确定这不是您第一次尝试。 And mode of 0 won't let you do much with it, unless you remove it. 除非您将其删除,否则模式0不会让您对其进行过多处理。 Also, do check your calls for errors, it will if not resolve, but, at least, amend your questions. 另外,请检查您的通话是否有错误,如果无法解决,则至少可以修正您的问题。

Permissions of 0 to sem_open mean that nobody can access the semaphore. sem_open权限为0表示没有人可以访问该信号量。 You really should add proper error checking -- it will tell you which function is failing and way. 您确实应该添加适当的错误检查-它会告诉您哪个函数失败了,以及方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM