OS X 上的 sem_init [英] sem_init on OS X
问题描述
我正在编写一些使用 pthread 和信号量库的代码.sem_init
函数在我的 Ubuntu 机器上运行良好,但在 OS X 上 sem_init
函数完全无效.图书馆有什么问题还是有不同的做法?这是我用来测试的代码.
I am working on some code which uses the pthread and semaphore libraries. The sem_init
function works fine on my Ubuntu machine, but on OS X the sem_init
function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test.
sem_t sem1;
sem_t sem2;
sem_t sem3;
sem_t sem4;
sem_t sem5;
sem_t sem6;
sem_init(&sem1, 1, 1);
sem_init(&sem2, 1, 2);
sem_init(&sem3, 1, 3);
sem_init(&sem4, 1, 4);
sem_init(&sem5, 1, 5);
sem_init(&sem6, 1, 6);
这些值似乎是随机数,并且在调用 sem_init
后不会改变.
The values appear to be random numbers, and they do not change after the sem_init
call.
推荐答案
不支持未命名信号量,需要使用命名信号量.
Unnamed semaphores are not supported, you need to use named semaphores.
要使用命名信号量代替未命名信号量,请使用sem_open
代替sem_init
,并使用sem_close
和sem_unlink
> 而不是 sem_destroy
.
To use named semaphores instead of unnamed semaphores, use sem_open
instead of sem_init
, and use sem_close
and sem_unlink
instead of sem_destroy
.
这篇关于OS X 上的 sem_init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!