I am trying to port a project (from linux) that uses Semaphores to Mac OS X however some of the posix semaphores are not implemented on Mac OS X
The one that I hit in this port is sem_timedwait()
I don’t know much about semaphores but from the man pages sem_wait() seems to be close to sem_timedwait and it is implemented
From the man pages
sem_timedwait()function shall
lock the semaphore referenced by
semas in thesem_wait()function.
However, if the semaphore cannot be
locked without waiting for another
process or thread to unlock the
semaphore by performing asem_post()
function, this wait shall be ter-
minated when the specified timeout
expires
From my limited understanding of how semphores work I can see that sem_timedwait() is safer, but I still should be able to use sem_wait()
Is this correct? If not what other alternatives do I have…
Thanks
It’s likely that the timeout is important to the operation of the algorithm. Therefore just using
sem_wait()might not work.You could use
sem_trywait(), which returns right away in all cases. You can then loop, and use a sleep interval that you choose, each time decrementing the total timeout until you either run out of timeout or the semaphore is acquired.A much better solution is to rewrite the algorithm to use a condition variable, and then you can use
pthread_cond_timedwait()to get the appropriate timeout.