Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing condition variables for CRITICAL_SECTIONs for Winthreads for XP

I have a desire for POSIX style condition variables on Win32. I have some code that needs to run on XP, so I can't use Vista/Server 2008's CONDITION_VARIABLE.

It makes use a of a pseudo condition variable class currently, which is based on leaving the critical section and signaling an auto reset event for signaling / waking. For waiting on the condition variable, it leaves the critical section, WaitForSingleObjects on the event, and then reenters the critical section. This is mostly fine, but it doesn't support broadcast, and may have other issues with regard to fairness, which I don't care too much about.

We do use boost, so I know know I could use boost threads or pthreads-win32, which support condition variables, but ideally I would like the interface to be such that I can drop in the Microsoft implementation when/if it becomes possible to use it directly. I have seen Strategies for Implenting POSIX Condition Variables on Win32, but the "best" (most correct) solution uses a Mutex and not a CRITICAL_SECTION. There is a sketch of an implementation with a CRITICAL_SECTION in the second part, but it is not complete, and the other solutions with CRITICAL_SECTIONs concern me due to the concerns outlined for them in the article.

In short, how do I implement a correct, not necessarily fair (but that would be nice), condition variable on win32 for critical sections such that I can drop in Microsoft's implementation when it becomes available to me?

like image 667
Logan Capaldo Avatar asked Aug 02 '09 11:08

Logan Capaldo


People also ask

How condition variable is implemented?

The implementation of condition variables involves several mutexes. Condition variables support three operations: wait - add calling thread to the queue and put it to sleep. signal - remove a thread form the queue and wake it up.

What is condition variable in synchronization?

Condition variables are synchronization primitives that enable threads to wait until a particular condition occurs. Condition variables are user-mode objects that cannot be shared across processes. Condition variables enable threads to atomically release a lock and enter the sleeping state.

What is condition variable in thread?

Condition variables are used to wait until a particular condition predicate becomes true. This condition predicate is set by another thread, usually the one that signals the condition. Condition wait semantics. A condition predicate must be protected by a mutex.

What is conditional variable in operating system?

A conditional variable in operating system programming is a special kind of variable that is used to determine if a certain condition has been met or not. It is used to communicate between threads when certain conditions become true. A conditional variable is like a queue.


1 Answers

The papers to which you refer were written by colleagues of mine as an outgrowth of our work on the ACE C++ framework and its OS wrapper facades. As mentioned in my bio, "I don't do Windows", but I still actively work on ACE, and I just took a look, and it appears that the condition variable implementation for Win32 uses CRITICAL_SECTION (on initial inspection it looks like it's just using a mutex, but if you dig deeper you'll find that there's a layer below in which a mutex is defined as CRITICAL_SECTION on windows platforms).

FYI, in order to insure that the code hadn't been modified to use the new Vista APIs, the code base I checked is actually a branch off the 1.5 line.

Presuming you just want a C API for condition variables, and not C++ wrappers around it, all of this should be contained in one set of files: ace/OS_NS_Thread.{h,inl,cpp} The license on ACE is very generous, and not GPL, so you can lift code from there into proprietary code base without fearing "GPL contamination".

You can obtain releases of ACE at http://download.dre.vanderbilt.edu/; the version I inspected is a commercially-supported release derived from ACE 5.5.2, maintained by OCI and available for download at http://www.theaceorb.com/downloads/1.5a/index.html.

In the interest of full disclosure, I've been a longtime user/contributor/maintainer to ACE, worked on that research staff for awhile, and am now an employee of OCI. I don't think that changes the utility/applicability of this code for you, nor do any of the aforementioned entities derive revenue from you lifting code from the source.

like image 163
Chris Cleeland Avatar answered Sep 21 '22 16:09

Chris Cleeland