-4

Possible Duplicate:
bathroom synchronization and queue of threads

I have looked at similar questions in stack and used google, they haven't helped me answer my question.For homework I have been given the bathroom synchronization problem. A shared bathroom, a female cannot use while a male is in there and vice versa. What I am trying to figure out is how to stop and restart thread if the other sex is in the bathroom. So far I have several conditions that if no one of the opposite sex is in there go and if not I tell the semaphore to wait. Then when leaving if no one of your sex is left in the restroom let the other sex in(semaphore to go). I don't know If I am having trouble with when to lock the threads or have the semaphores to wait. Here is my code.

//my variables

 sem_t male;
 sem_t female;
 int maleInBath;
 int femaleInBath;
 pthread_mutex_t coutMutex;

//initialize the variables

void personInitGlobals()
{
   // LEAVE THIS STATEMENT                                                 
   pthread_mutex_init(&coutMutex, NULL);

   // TODO: Complete this function                                         
   int init=0;
   maleInBath=0;
   femaleInBath=0;
   sem_init(&male, 0, init);
   sem_init(&female, 0, init);
}

//enter restroom

void 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 && maleInBath==0){
     femaleInBath++;
    }else if(isFemale && maleInBath >0){
      sem_wait(&female);
   }else if(!isFemale && femaleInBath==0){
      maleInBath++;
  }else{
    sem_wait(&male);
  }
}

person leaves the restroom

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

   // TODO: Complete this function                                         
   if(isFemale){
     femaleInBath--;
    if(femaleInBath==0){
      sem_post(&male);

    }
   }else{
     maleInBath--;
     if(maleInBath==0){
     sem_post(&female);

  }
}

}

Community
  • 1
  • 1
Aaron
  • 4,380
  • 19
  • 85
  • 141

1 Answers1

0

Well as awoodland already have linked to the other thread on SO, so its good to have a look on it. Just in case to avoid duplicated solutions to same problem in same class, I will also ask you whether use of semaphore is mandatory??

If not, then just create a queue (first in first go) structure and put every incomming person to queue. When new person comes, apply your bathroom availability logic. Also when a person leaves the bathroom, apply the bathroom logic again if the queue is not empty. And thats it for you.

  • We have to use semaphores. I have looked at the other threads, I think there is something about the logic that I just do not understand – Aaron May 08 '12 at 15:19