1

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);
         }
     }

}
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Aaron
  • 4,380
  • 19
  • 85
  • 141
  • I think you want your queues to contain the ids and not the threads. – Vaughn Cato May 06 '12 at 22:09
  • Actually you could make this simpler by just holding a boolean to indicate if the current people in the toilet are male or female and then just a counter. You need a synchronisation lock around the boolean and the counter so you don't need to hold a counter of men or women unless you want to hold a queue waiting, in which case you could use some form of queue and pop off when they are allowed to enter the restroom – EdChum May 06 '12 at 22:18

1 Answers1

1

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);
LihO
  • 41,190
  • 11
  • 99
  • 167