The rules
A single bathroom may hold several people, but everyone inside must be the same category, say men or women. Two constraints apply: no mixing of categories, and a bounded number of occupants if capacity is limited.
Why it is interesting
This generalises the readers writers idea to two competing reader classes. It is essentially mutual exclusion between groups while allowing concurrency within a group. The state machine has an empty state, a men occupied state, and a women occupied state.
A working approach
Track the current category and a count of people inside under a mutex:
- An arriving person of the matching category or an empty room enters and increments the count.
- An arriving person of the other category waits until the count reaches zero.
- The last person to leave clears the category and signals waiters.
Avoiding starvation
If men keep arriving they can hold the room forever and starve women. A turnstile or alternating policy that blocks new same category arrivals when the other category is waiting restores fairness, just like writer preference in readers writers.
Key idea
The unisex bathroom enforces same category occupancy like grouped mutual exclusion, and a turnstile prevents one category from starving the other.