R – Implementing condition variables for CRITICAL_SECTIONs for Winthreads for XP

cmultithreadingwinapi

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?

Best Answer

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.