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
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
.