Sem_getvalue () functionality on Mac OS X - C ++

I am trying to implement synchronized use of shared memory for a bunch of threads in Mac OS X using semaphores.

(I just forget that Mac users have a lot of problems initializing the semaphore and destroying it ... that can be fixed with sem_open () and sem_unlink ()): D

But apparently there is nothing to get the current value of the semaphore except sem_getvalue (), which was not implemented in mac os x.

Any suggestion for someone who doesn't have a Linux operating system and needs to download their homework in hours ?? :)

thank

+3
c ++ semaphore macos
May 20 '13 at 17:44
source share
1 answer

I think you are asking: "How can I get around the lack of sem_getvalue() in OS X?"

I can think of three approaches:

First (and best of all, in my opinion) redesign your program so that the current semaphore value is never required. After all, as the documentation warns , the value reported by sem_getvalue will not necessarily be accurate at the time it is received.

Secondly, if necessary, wrap the POSIX semaphore functions and save your own score. Each sem_t can be decorated with a counter and a mutex protecting this counter. Your implementation will probably (and probably should be!) The same caveat as sem_getvalue , i.e. It cannot be considered that the counter may be accurate after receiving it.

Third, and in my opinion the least acceptable, switch to the older and more complex SysV IPC semaphore interface . This implements something similar to sem_getvalue .

+7
May 20 '13 at 18:05
source share
— -



All Articles