[开发随笔]在OS X下,sem_init失败

Posted on 2012-01-31 18:15  Jophoenix  阅读(1128)  评论(0编辑  收藏  举报

在开发Iphone上的项目时,原本运行的好好的程序,移植到Iphone上就死锁了,最后发现是创建信号量失败。

sem_init返回-1

 

原因是:

OS X不支持创建无名的信号量,只能使用sem_open创建有名的信号量。

"<semaphore.h> declares sem_init so that it compiles properly on OS X, but it returns -1 with errno set to ENOSYS (function not implemented)."

创建:

sem_t* psemaphore = sem_open("/mysem",O_CREAT, S_IRUSR | S_IWUSR, 10);

 

销毁:

 sem_close(psemaphore);
 sem_unlink("/mysem");

17加油!