sem_getvalue() dysfunctionality in Mac OS X - C++

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:04:59

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/16655153/sem-getvalue-dysfunctionality-in-mac-os-x-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!