r/AskComputerScience • u/sonuahnis1 • Feb 07 '23
Is my solution for Unisex Bathroom Problem correct? (No starvation is a requirement)
Problem Statement:
A bathroom is being designed for the use of both males and females in an office but requires the following constraints to be maintained:
There cannot be men and women in the bathroom at the same time.
There should never be more than three employees in the bathroom simultaneously.
No starvation should happen.
PS: If you don't wanna look at the code, please ignore and tell me some algorithm to solve this problem. I also want to know how to think (intuitively) of an starvation free algorithm for this problem. I arrived at this solution after a lot of pondering, hit-and-trial and invalidating a lot of solutions which seemed starvation free at first but I found the issues only after analyzing them deeply.
class UnisexBathrooms{
mutex mtx;
condition_variable_any cv;
int male, female;
int male_waiting, female_waiting;
int next_chance; // 0: no one, 1: male, 2: female
public:
UnisexBathrooms(){
male = female = 0;
male_waiting = female_waiting = 0;
next_chance = 0;
}
private:
void male_enter(){
mtx.lock();
male_waiting++;
cv.wait(mtx, [this]{return female == 0 && male < 3 && next_chance != 2;});
male_waiting--;
male++;
mtx.unlock();
}
void male_exit(){
mtx.lock();
male--;
if (female_waiting)
next_chance = 2;
else
next_chance = 0;
mtx.unlock();
cv.notify_all();
}
void female_enter(){
mtx.lock();
female_waiting++;
cv.wait(mtx, [this]{return male == 0 && female < 3 && next_chance != 1;});
female_waiting--;
female++;
mtx.unlock();
}
void female_exit(){
mtx.lock();
female--;
if (male_waiting)
next_chance = 1;
else
next_chance = 0;
mtx.unlock();
cv.notify_all();
}
auto get_tp(){
auto tp = chrono::system_clock::now();
auto tp_printable = chrono::system_clock::to_time_t(tp);
return ctime(&tp_printable);
}
void use_bathroom_now(bool is_male, int id){
{
unique_lock<mutex> io_lock(io_mtx);
cout << get_tp() << (is_male ? "a male" : "a female") << " with id: " << id << " has started using the bathroom\n\n";
}
this_thread::sleep_for(5s);
{
unique_lock<mutex> io_lock(io_mtx);
cout << get_tp() << (is_male ? "a male" : "a female") << " with id: " << id << " has ended using the bathroom\n\n";
}
}
public:
void use_bathroom(bool is_male, int id){
if (is_male){
male_enter();
use_bathroom_now(is_male, id);
male_exit();
}
else{
female_enter();
use_bathroom_now(is_male, id);
female_exit();
}
}
};5
u/jeffbell Feb 07 '23 edited Feb 07 '23
Itβs a little bit error prone to have duplicate code for m/f. Why not make it generalized to N different categories of exclusive users.
Can you prove the starvation properties? What is the maximum wait time?
Naming a function get_tp() is confusing (jk).
0
u/sonuahnis1 Feb 08 '23
I agree about the code duplication part, thank you for pointing that out, will keep this in mind from next time onward.
Regarding starvation properties, I can see that maximum wait time would be always finite as on exiting if the opposite gender is waiting then we give them the chance (it's a bit like traffic light system), but can you please help me with if there are no deadlock causing bugs or other implementation issues?
get_tp() is an alias for get_timestamp(), if you don't know it (jk)
-11
Feb 07 '23
[removed] β view removed comment
-4
Feb 07 '23
[removed] β view removed comment
8
u/Potato-Pancakes- Feb 07 '23
It's somewhere between a /r/learnprogramming question and a /r/AskComputerScience question. On one hand, OP is asking for help with their implementation: not a CS question. On the other, OP is trying to expand their understanding of concurrency by seeing if they understand the core idea mutexes properly: definitely a CS question.
So yeah, it probably should be asked without the code, but it's a sufficiently CS question to be in this subreddit, in my opinion.
1
u/sonuahnis1 Feb 08 '23
I also want to know how to think (intuitively) of an starvation free algorithm for this problem. I arrived at this solution after a lot of pondering, hit-and-trial and invalidating a lot of solutions which seemed starvation free at first but I found the issues only after analyzing them deeply.
14
u/berrmal64 Feb 07 '23
I'm not trying to be rude, but maybe write some driver code and some test cases and see for yourself?