Originally Posted by
vart
how do you synchronize the usage of the "restroom" by threads?
Show the code of the thread main function
I am using mutex locks and ocndition variables. I am 100% sure that it is not deadlocking because of a thread holding a lock. I have debug output and its like ALL threads have died.
In the function with sleep it will make it to person sleeping but when it hangs I never get the person waking up debug. For some reason the thread will not wake up. Again this doesn't happen all the time and sometimes it just sleeps for up to 10 times longer than it should
Main function of thread
Code:
void *OnePerson(void *threadarg) {
struct person *newPerson;
newPerson = (struct person *) threadarg;
const int use_fac_time=5;
int gender = newPerson->gender ;
int id = newPerson->id;
Arrive(id,gender);
UseFacilities(id,gender,use_fac_time);
Depart(id,gender);
}//end OnePerson
Thread with sleep
Code:
void UseFacilities(int id, int gender, int time){
printf("Person using restroom:%d\nGender:%d\nNumber in Restroom:%d\nMen Waiting:%d\nWomen Waiting:%d\n"
"-------------------------------------\n",id,gender,num_in_restroom,num_men_waiting,num_women_waiting);
printf("Person %d sleeping\n", id);
sleep(time);
printf("Person %d waking\n", id);
}//end UseFacilities