In C#, how do I handle critical section with two different "rights of way"?
Theoretical use case: imagine a swimming pool (the resource). Many individual swimmers (worker threads A, B, C, and D) can swim into the pool as they feel like to, and its totally OK for multiple swimmers to be in the pool concurrently. However, from time to time there is a private swimming class (another worker thread, E), and the class has rights above all other swimmers. When a class starts, all other swimmers are asked to complete their task (and exit the pool ASAP).
Swimmers who were asked to leave the pool have already taken their shower and left the premise, as technically, they had already completed their task. Any individual swimmer who arrived after the class started must sit on the bench and wait until the class is over before they can swim.
Dealing with concurrency I am used to the
lock statement but in this case this would result in only one swimmer (thread) at any time, which is not acceptable.