[
Let’s dry run the above code. Say thread A executes
efficient_waiting_function()
first and finds the loop predicate is false and enters the loop. Next, thread A executes the statement cond_var.wait()
and is placed in a wait queue. At the same time thread A gives up the mutex. Now thread B comes along and executes change_predicate()
method. Since the mutex was given up by thread A , thread B is able to acquire it and set the predicate to true. Next, it signals the condition variable cond_var.signal()
. This step places thread A into the ready queue but thread A doesn’t start executing until thread B has released the mutex.]
can you please explain why, after placing in the wait queue, gives up the mutex after placing in the wait queue?