I have a problem understanding mutex, For example let's consider the following code from my book:
int students[2] = {0};
cond_t conds[2];
mutex_t global;
void onArrival(int faculty) {
mutex_lock(&global);
int other = facolty ? 0:1;
while(students[other]>0) cond_wait(&conds[facolty],&global);
students[facolty]++;
mutex_unlock(&global);
}
void onLeave(int faculty) {
mutex_lock(&global);
students[facolty]--;
int other = facolty ? 0:1;
cond_broadcast(&conds[other]);
mutex_unlock(&global);
}
cond_broadcast
will wake up all waiting threads but the real question is for how long? I mean it may happen that line 16 was executed which woke up 1000 threads but when they try to catch the mutex they fail because line 17 wasn't executed yet...
So what will happen in this case?
Threads go back to sleep and when doing line 17 none will wake up?
Or, They will do busy waiting until the mutex in released?