Condition_variable of C + + standard library

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.