Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 610
Morteza Farhang Avatar asked May 20 '13 17:05

Morteza Farhang


1 Answers

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.

like image 144
pilcrow Avatar answered Nov 06 '22 13:11

pilcrow