在Mac OS X - C ++中的sem_getvalue()功能障碍 [英] sem_getvalue() dysfunctionality in Mac OS X - C++

查看:732
本文介绍了在Mac OS X - C ++中的sem_getvalue()功能障碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在Mac OS X中通过信号量同步使用一个共享内存来共享线程。我只是忽略了Mac用户有很多问题,初始化一个信号量并销毁它...,可以通过sem_open()和sem_unlink()修复:D



但显然是为了semaphore的当前值有没有什么,但没有在mac os x中实现的sem_getvalue()。



任何建议,没有linux操作系统运行的人,应该上传:



感谢

解决方案

我想你在问:我如何在OS X上 sem_getvalue()不存在?



我可以想到三种方法:



首先(我认为最好)重新设计程序,是永远不需要的。毕竟,文档警告 sem_getvalue 在接收时不一定准确。



其次,如果需要,包装POSIX信号量函数并保留自己的计数。每个 sem_t 可以由计数器和互斥量保护该计数器装饰。你的实现可能会有(也许应该有)与 sem_getvalue 相同的警告,也就是说,计数一旦被检索就不能被信任。 / p>

第三,在我看来,最不受欢迎的,切换到较旧的和更复杂的SysV信号量IPC 界面。它实现类似于 sem_getvalue 的东西。


I'm trying to implement synchronized use of a shared memory for bunch of threads in Mac OS X by semaphores.

(I just overlook the fact that Mac users have got lots of issues with initializing a semaphore and destroying it...,which can be fixed by sem_open() and sem_unlink()):D

But apparently for getting semaphore's current value there's nothing but sem_getvalue() which hasn't been implemented in mac os x.

Any suggestion for someone that doesn't have linux OS running and should upload his homework in hours??!:)

Thanks

解决方案

I think you are asking, "how can I work around the absence of sem_getvalue() on OS X?"

I can think of three approaches:

First (and best, in my opinion) redesign your program so that the current value of the semaphore is never needed. After all, as the documentation warns, the value reported by sem_getvalue isn't necessarily accurate by the time it is received.

Second, if necessary, wrap the POSIX semaphore functions and keep your own count. Every sem_t can be adorned by a counter and a mutex protecting that counter. Your implementation probably will have (and probably should have!) the same caveat as sem_getvalue, that is, the count can't be trusted to be accurate once it is retrieved.

Third, and least palatable in my opinion, switch to the older and more complicated SysV semaphore IPC interface. That implements something analogous to sem_getvalue.

这篇关于在Mac OS X - C ++中的sem_getvalue()功能障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆