I seem to be at a loss on how to solve this problem...
Let me try explaining it via a model.
There are two types of threads: F and M. Both types of threads execute the same function.
M threads may not run while there are F threads running and vice versa.
So the question is... how do we implement this entirely with semaphores?
I have been thinking something like
Now, there are three semaphores (they must be semaphores!): F, M and E.
(OppositeSemaphore = F for M threads and M for F threads, and GenderSemaphore = F for F threads and M for M threads.)Code:OppositeSemaphore->down(); if (++(*GenderCount) == 1) GenderSemaphore->down(); OppositeSemaphore->up();
(Obviously the last of both types of threads will do up on the semaphore once they leave the function.)
...but of course, this won't work.
An extra level of semaphores won't work either since we can't block with mutual exclusion while waiting for another semaphore (we'll block all threads!).
So thinking about it... I am at a loss right now.