C ++ state variable and waiting

I am trying to understand what this line of code is doing. Will it pause or wait or run code until the count is less than 6?

unique_lock<mutex> lck(mtx);
 cv.wait(lck, [this]() { return count <6; });



source to share

1 answer

He will wait until he receives the message cv

that count < 6

. From reference :

Equivalent to

while (!pred()) {


This overload can be used to ignore false wakes while waiting for a certain condition to become true. Please note that before entering this method, you need to acquire the lock, after it wait(lock)

exits, it will also be restored, i.e. Blocking can be used as access protection pred()


You can think of a predicate as a completion state to stop waiting.



All Articles