I need to resolve a synchronization problem using PThreads.
At some point in the code, one thread needs to know the number of threads blocked on a semaphore (as defined in sempahore.h
).
When I took a look at the man pages of sem_getvalue(sem_t *s, int *sval)
, it was stated that the returned value sval
is 0
in Linux, but may have other semantics according to POSIX, i.e. the absolute value of sval
is set to the number of threads blocked on semaphore s
.
So I am looking for a Linux compiling option to make possible these alternative semantics.
I tried to simulate the number of threads blocked on a certain semaphore by keeping trace of an integer variable each time a thread executes sem_wait()
on that semaphore.
But I am wondering about the correctness of this method especially for the race conditions.