Suppose our threads look something like this:
thread1:
Lock(mutex) {
//code
if(something)
signal(condition_var);
//code
} //unlocks
thread2:
Lock(mutex) {
//code
while(something)
wait(mutex, condition_var);
//code
} //unlocks
1) Suppose the signal in thread1 gets executed and then context switch happens before mutex gets unlocked.
2) thread2 which was waiting on condition_var, attempts to continue but mutex is still locked. Now it will keep waiting i guess. Suppose context happens again.
3) thread1 now finishes and unlocks mutex.
How will wait in thread2 behave now, as anything won't signal it again, how will thread2 stop waiting?
signal()
. In that case, instead of waiting on condition variable, thread begins to wait on mutex. This waiting ends when someone unlock mutex(at the step 3 in you example). – Tsyvarev