Implementation of conditional variables in Windows

Source: Internet
Author: User

What is a condition variable?

Is a synchronization object.

 

What is the use of conditional variables?

It is used in complex, multi-threaded, multi-core programs to implement synchronization tasks between multiple threads.

 

What is the difference between conditional variables and other synchronization objects?

The biggest difference between conditional variables and synchronization objects such as events, mutex locks, and segment is that"ConditionThe "two-character, synchronization of other synchronization objects" condition "is fixed, such as the event is triggered, the mutex lock is released, and the" condition "of the condition variable is completely customized, for example, you can synchronize the condition variables when "Michael Jacob made 5 yuan, Li Si watched TV, and Obama accessed Mart. Therefore, conditional variables can be used for complex synchronization tasks.

 

Is there any conditional variable in windows?

The simple answer is: No. Windows API does not provide a conditional variable object. This is the reason for this article and the problem to be solved.

The answer to the complexity is:

  1. Use Versions later than Windows Vista (Versions later than Vista provide native conditional variable objects;
  2. Extract from open source library;
  3. You can implement it yourself;

 

Solution 1 is unrealistic, because most of your customers currently use Windows XP/2003 or earlier versions, and Vista is not selling well. solution 2 can refer to the ace library, however, it is difficult to extract and use too many conditional macros and irrelevant code (you cannot drag the variable into the entire large ace library for synchronization); solution 3 is more difficult, I must be familiar with multi-threaded programming and consider many abnormal details. I just adopted solution 3-self-implemented, because there is no ready-made online and I have to do it! And you don't have to re-create the wheel. You can simply copy the following code to your project (as long as your project is C ++ ).

 

The implementation code is as follows:

 

Class my_mutex <br/>{< br/> Public: <br/> my_mutex (bool be_initial_owner = false) <br/>{< br/> mutex _ = createmutexa (null, be_initial_owner, null); <br/>}< br/> ~ My_mutex (void) <br/>{< br/> closehandle (mutex _); <br/>}< br/> Public: <br/> int acquire (void) <br/>{< br/> DWORD ret = waitforsingleobject (mutex _, infinite); <br/> return ret = wait_object_0? 0:-1; <br/>}< br/> int release (void) <br/>{< br/> bool Bret = releasemutex (mutex _); <br/> return Bret? 0:-1; <br/>}< br/> handle (void) <br/>{< br/> return mutex _; <br/>}< br/> protected: <br/> handle mutex _; <br/> }; <br/> class my_semaphore <br/>{< br/> Public: <br/> my_semaphore (long init_count, long max_count = (STD: numeric_limits <long> :: max) () <br/>{< br/> assert (init_count >=0 & max_count> 0 & init_count <= max_count ); <br/> Sema _ = createsemaphorea (null, init_count, max_count, null ); <Br/>}< br/> ~ My_semaphore (void) <br/>{< br/> closehandle (SEMA _); <br/>}< br/> public: <br/> int post (Long Count = 1) <br/>{< br/> bool Bret = releasesemaphore (SEMA _, Count, null ); <br/> return Bret? 0:-1; <br/>}< br/> int wait (long timeout =-1) <br/>{< br/> DWORD ret = waitforsingleobject (SEMA _, timeout); <br/> return ret = wait_object_0? 0:-1; <br/>}< br/> handle (void) <br/>{< br/> return Sema _; <br/>}< br/> protected: <br/> handle Sema _; <br/> }; <br/> template <typename mutex> <br/> class my_condition <br/>{< br/> Public: <br/> my_condition (mutex & M) <br/>: mutex _ (m) <br/>, waiters _ (0) <br/>, Sema _ (0) <br/>{< br/>}< br/> ~ My_condition (void) <br/>{< br/>}< br/> Public: <br/> // returns a reference to the underlying mutex _; <br/> mutex & mutex (void) <br/>{< br/> return mutex _; <br/>}< br/> // signal one waiting thread. <br/> int signal (void) <br/> {<br/> // must hold the external mutex before enter <br/> If (waiters _> 0) <br/> Sema _. post (); <br/> return 0; <br/>}< br/> // signal * All * Waiting threads. <br/> int Broadcast (Void) <br/>{< br/> // must hold the external mutex before enter <br/> If (waiters _> 0) <br/> Sema _. post (waiters _); <br/> return 0; <br/>}< br/> int wait (unsigned long wait_time =-1) <br/> {<br/> // must hold the external mutex before enter <br/> int ret = 0; <br/> waiters _ ++; <br/> ret = signalobjectandwait (mutex _. handle (), Sema _. handle (), wait_time, false); <br/> mutex _. acquire (); <br/> waiters _ --; <Br/> return ret = wait_object_0? 0:-1; <br/>}< br/> protected: <br/> mutex & mutex _; <br/> // Number of waiting threads. <br/> long waiters _; <br/> // queue up threads waiting for the condition to become signaled. <br/> my_semaphore Sema _; <br/>}; <br/> 

 

The above code is implemented using a template. The template parameter of the variable class my_condition is the mutex type used in combination with the condition variable. To facilitate direct use, I also provide the mutex type: my_mutex.

 

I have tested and used the code in the project. If you find any problems, you are welcome to criticize and correct them.

 

 

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.