Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:06+00:00 2026-06-03T17:23:06+00:00

For homework we have been given the bathroom synchronization problem. I have been struggling

  • 0

For homework we have been given the bathroom synchronization problem. I have been struggling trying to figure out how to start. What I would like to do when a person enter the restroom(personEnterRestrrom function), if they are female and no males are in the restroom they enter,if not they go into a queue for women waiting. I want to do the same for men. I tried to implement a queue that holds thread, but cannot get it to work. Then in personLeavesRestroom function. When a person leaves if no one is left in the bathroom the other queue starts. Here is my code, I know I am far off, by I do need some guidance and am not very familiar with semaphores.

//declarations
pthread_mutex_t coutMutex;
int menInBath;
int womanInBath;
int menWaiting;
int womenWaiting;
queue<pthread_mutex_t>men;
queue<pthread_mutex_t>women;


 personEnterRestroom(int id, bool isFemale)
 {
   // LEAVE THESE STATEMENTS                                                 
   pthread_mutex_lock(&coutMutex);
  cout << "Enter: " << id << (isFemale ? " (female)" : " (male)") << endl;
  pthread_mutex_unlock(&coutMutex);

  // TODO: Complete this function                                           
 if(isFemale && menInBath<=0)
  {
     womanInBath++;
   }
 else if(isFemale && menInBath>0)
 {
  wait(coutMutex);
  women.push(coutMutex);
}
 else if(!isFemale && womanInBath<=0)
{
  menInBath++;
}
else
{
  wait(coutMutex);
  men.push(coutMutex);
}

}

   void
    personLeaveRestroom(int id, bool isFemale)  
    {
   // LEAVE THESE STATEMENTS                                                 
    pthread_mutex_lock(&coutMutex);
    cout << "Leave: " << id << (isFemale ? " (female)" : " (male)") << endl;
    pthread_mutex_unlock(&coutMutex);

  if(isFemale)
    womanInBath--;
  if(womanInBath==0)
    {
       while(!men.empty())
         {
           coutMutex=men.front();
           men.pop();
           signal(coutMutex);
         }
     }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:23:09+00:00Added an answer on June 3, 2026 at 5:23 pm

    If you are looking for FIFO mutex, this one could help you:

    You gonna need:
    mutex (pthread_mutex_t mutex),
    array of condition variables (std::vector<pthread_cond_t> cond)
    and queue for storing thread IDs (std::queue<int> fifo).

    Let’s say there is N threads with IDs 0 to N-1. Then fifo_lock() and fifo_unlock() could look like this (pseudocode):

    fifo_lock()
        tid = ID of this thread;
        mutex_lock(mutex);
        fifo.push(tid); // puts this thread at the end of queue
    
        // make sure that first thread in queue owns the mutex:
        while (fifo.front() != tid)
            cond_wait(cond[tid], mutex);
    
        mutex_unlock(mutex);
    
    fifo_unlock()
        mutex_lock(mutex);
        fifo.pop(); // removes this thread from queue
    
        // "wake up" first thread in queue:
        if (!fifo.empty())
            cond_signal(cond[fifo.front()]);
    
        mutex_unlock(mutex);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am kind of stumped here and have been trying to figure this out
I have a homework question that says: Problem 1: Given the array [ 22
I have been given a homework assignment where I need to create a captcha
This is a homework problem that I have. I have been doing some research
I have been trying to implement an efficient string comparing algorithm that will given
Before I start, yes this is a homework. I would not have posted here
First of all this is homework. I have been trying to get rid of
So I have been working on properly implementing a quicksort algorithm for a homework
We have started learning Java in school, and we have been given a few
This is a homework question, binary search has already been introduced: Given two arrays,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.