I seem to recall a teacher saying that sem_getvalue() could be dangerous. Is this true? If so, why? In what circumstances?
I'm asking you this question because I'm currently doing an assignment where I need to use named semaphores (yes, the POSIX ones). I wanted to have a named semaphore whose behaviour would be equal to that of a mutex - either its value is 0 or 1, but never higher than 1. Here's what I wanted to do (this code is merely representative):
I hope this doesn't sound too awkward...Code:void my_sem_post(my_named_semaphore) { sem_wait(can_update_named_semaphore); if (sem_getvalue(my_named_semaphore) == 0) { sem_post(my_named_semaphore); } sem_post(can_update_named_semaphore); }