Windows thread Synchronization "5" Condition variable (Condition Variable)

Source: Internet
Author: User

First, Introduction

Suppose there is a task that I and Zhang San have done together. Zhang San sent the manuscript after the initial review into a queue, I am responsible for the document in this queue for approval, decided to publish or not. Zhang San it takes 15 minutes to review a document, I need 2 minutes to process a document.

If Zhang San and I were to be considered two threads, we shared a queue of data. In accordance with the general multi-threaded thinking, he put the data in the queue at intervals, I check the queue every time there is data, if any, then deal with it.

If we work in the way above, then most of the time, I'm just waiting, so this is a relatively inefficient way.

But in a different way, the situation is much better. He put a document into the queue, check the number of documents in the queue is 20, if so, notify me to deal with. After I have handled it, because my processing speed is faster, so after a period of time, the queue will become empty, I will leave. In this way, I can make more efficient use of time, for example, to use spare time to clean up, organize documents or do other things.

Sometimes, we should not let a thread "busy waiting", but should let it hibernate (sleep) to save the computer resources, until a condition is satisfied, then continue to work.

The above can be achieved with the condition variable.

The condition variable has two states: established and not established .


Second, initialize

Before you can use a conditional variable, you need to define a variable of type condition_variable:

Condition_variable CV;

There are 5 functions related to the condition variable;

Initializeconditionvariablesleepconditionvariablecssleepconditionvariablesrwwakeconditionvariablewakeallconditionvariable

For conditional variables, you cannot read and write directly in the code, and all operations must be done through the above functions .

Conditional variables must be initialized before they are used, by:

Initializeconditionvariable (&CV);

You do not need to manually destroy a condition variable when it is no longer needed by the program.


Three, waiting

The wait condition variable can be used with the Sleepconditionvariablecs or SLEEPCONDITIONVARIABLESRW function.

Sleepconditionvariablecs (&CV, &cs, dwtimeout);

The first parameter is a pointer to a condition variable, the second is a critical section (critical_section) pointer, and the third parameter is a timeout, in milliseconds.

The function is to unlock CS and wait for the CV to be established, and after the function returns, the CS will be locked again and CS will not be set. During the wait, the current thread sleeps until it wakes up (see the function used to wake up, as described later).

SLEEPCONDITIONVARIABLESRW usage is similar, see here:

Https://msdn.microsoft.com/en-us/library/windows/desktop/ms686304.aspx


Iv. Notification (Wakeup)

Wakeconditionvariable (&CV);

Use Wakeconditionvariable to set the status of the CV to set up and wake up one of those threads waiting for the CV.

Wakeallconditionvariable (&CV);

Use Wakeallconditionvariable to set the CV status to set up and wake up all those threads waiting for CVS.

Windows thread Synchronization "5" Condition variable (Condition Variable)

Related Article

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.