educative.io

Educative

Is the mutex release statement missing here?

Is there a missing mutex lock release staement in this example code in the course?

void efficientWaitingFunction() {
  mutex.acquire() 	
  while (predicate == false) { // MISSING mutex release statement if predicate is false
    condVar.wait()
  }
  // Do something useful
  mutex.release()
}


void changePredicate() { 
  mutex.acquire()
  set predicate = true
  condVar.signal()
  mutex.release()
}
1 Like

Hi stuxnet,
No, the above code is not missing a release statement. In that lesson it is already mentioned below condition variables heading that " The wait() method when called on the condition variable will cause the associated mutex to be atomically released, and the calling thread would be placed in a wait queue ." The above code was given to just emphasize on the importance of using mutex with conditional loops .

1 Like