Conditon_variable (condition variable) for inter-thread synchronization
Condition_variable has 5 functions, function names and corresponding functions as follows:
Wait to block yourself, waiting to wake up
Wait_for block himself, wait for wake up, wait for a while
Wait_until blocks itself, waits to wake up, waits up to a certain point in time
Notify_one wakes up a thread waiting on this condition variable
Notify_all wakes all the threads waiting on this condition variable
The 3 wait functions require the entry of a locked unique_lock<mutex> variable, and there are two versions, one version accepts one comparator and the other does not. Use wait to illustrate:
void Wait (unique_lock<mutex> &lck);
Template<class predicate> void Wait (unique_lock<mutex> &lck,predicate pred);
The second version is similar to while (!pred ()) Wait (LCK) when multiple threads wait for a wake-up and contention resource at the same time; If the standard library does not provide this version, we can only write the code ourselves.
Condition_variable_any and condition_variable are basically the same, the difference is only the condition_variable requirements of the mutex can only be used unique_lock<mutex>, And Condition_variable_any is not limited.
Condition_variable of C + + standard library