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

前端 未结 1 2009
花落未央
花落未央 2020-12-06 21:04

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 o

相关标签:
1条回答
  • 2020-12-06 21:53

    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.

    0 讨论(0)
提交回复
热议问题