Windows via C/C ++ Study Notes-"conditional variables" for "thread synchronization" in user mode"

Source: Internet
Author: User

Condition variables -- condition variable is a newly added mechanism for processing thread synchronization issues in Windows Vista. It can be used with "critical section" or "srwlock" to synchronize threads, this is especially effective when implementing issues similar to "producer-consumer.

If there is no "product" available for "Consumer thread" (Reader thread), the "Consumer thread" should release the corresponding read/write lock or key code segment, then wait until a new "product" is created by the "producer thread" to continue running.

If a data structure used to store "Products" is full (such as an array), the corresponding "producer thread" needs to release the related locks and key code segments, at the same time, wait for the "consumer" to finish consuming these "products ".

 

The Conditional Variable mechanism is a thread synchronization mechanism designed to simplify the aforementioned "producer-consumer" problem. When a thread needs to release the lock on the resource in an atomic way and needs to be blocked until a condition is met, you can call the following function:

Bool sleepconditionvariablecs (
Pcondition_variable pconditionvariable,
Pcritical_section pcriticalsection,
DWORD dwmilliseconds ); Bool sleepconditionvariablesrw (
Pcondition_variable pconditionvariable,
Psrwlock,
DWORD dwmilliseconds,
Ulong flags );

 

As the function name implies, the first function is for the key code segment, and the second function is for the read/write lock.

The pcontidionvariable parameter of the 1st parameter points to an initialized condition variable that specifies the condition variable of the caller (a thread). The parameter type is the pointer of condition_variable.

The 2nd parameters indicate a "key code segment" or "read/write lock", which is used to protect shared resources.

The 3rd parameter dwmilliseconds specifies how long your thread will wait, and you can pass infinite to indicate that you need to wait indefinitely.

The 4th parameter flags of the second function specifies the requirements of the corresponding lock when the condition variable is met (that is, the lock should be set when the return value is returned, and what type of lock should the lock be): In the "producer thread" (writer thread), you should pass 0 to this parameter to indicate that the lock is an exclusive lock, which is exclusively occupied by the thread; in the "Consumer thread" (Reader thread), you should pass condition_variable_lockmode_shared to indicate to this parameter that the lock is a "shared lock" and that the lock can serve the "Consumer thread" in a shared manner.

When this parameter is called, the key code segment or read/write lock specified by the second parameter will be released so that the corresponding thread can access shared resources, so as to "produce" or "consume"; when the function returns, this lock will be set again. If srwlock is returned, set the read/write lock type based on the flags parameter: exclusive lock or shared lock. Key code segments are automatically set because key code segments are always exclusive.

If the wait times out, the function returns false; otherwise, the function returns true.

 

When a thread is blocked by sleepconditionvariablecs or sleepconditionvariablesrw, it can be awakened by another thread by calling the wakeconditionvariable or wakeallconditionvariable function.

Void wakeconditionvariable (
Pcondition_variable conditionvariable); // condition variable pointer Void wakeallconditionvariable (
Pcondition_variable conditionvariable); // condition variable pointer

 

When you call the wakeconditionvariable function, pass a pointer to the conditional variable. In this case, the conditional variable receives a signal within the sleepconditionvariable function of a thread waiting for the same conditional variable, notification thread. This function will return and set the corresponding lock to the desired type.

When you call the wakeallconditionvarialbe function, too many threads waiting on the same conditional variable will be awakened. It is okay to wake up multiple threads, but you need to set the parameter flags when calling the sleepconditionvariable * function: Pass 0 to the "producer thread", and pass condition_variable_lockmode_shared to the "Consumer thread. Therefore, in some cases, all "Consumer Threads" are awakened. Or wake up: producer, consumer, producer, consumer ...... This loop.

 

This book also provides an example. I will not talk about it here. I personally summarize that using conditional variables should follow the following pattern (I think so. If there is a mistake, please point it out)

 

Condition_variable g_cvproduce; // production condition variable
Condition_variable g_cvconsume; // consumption condition variable
Srwlock g_srwlock; // read/write lock

DWORD winapi consumer (pvoid pvparam) // consumer thread function
{

Acquiresrwlockshard (& g_srwlock); // request a shared lock (read lock)
Sleepconditionvariablesrw (g_cvconsume, & g_srwlock, infinite, condition_variable_lockmode_shared); // wait for the condition variable, which will be awakened by the producer thread

// Consume

Releasesrwlockshared (& g_srwlock); // release the shared lock
Wakeconditionvariable (& g_cvproduce); // wake up a producer thread

}

DWORD winapi producer (pvoid pvparam) // producer thread function
{

Acquiresrwlockexclusive (& g_srwlock); // requires an exclusive lock (write lock)

 

// The waiting condition variable is trusted and will be awakened by the consumer thread
Sleepconditionvariablesrw (g_cvproduce, & g_srwlock, infinite, 0 );

// Production

Releasesrwlockexclusive (& g_srwlock); // release the exclusive lock
Wakeallconditionvariable (& g_cvconsume); // wake up all consumer threads

}

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.