Windows Vista has some great new features for concurrent programming. For those of you still writing native code, it’s worth checking them out. For those writing managed code, we have a bunch of great stuff in the pipeline for the future, but unfortunately you’ll have to wait. Or convert (back) to the dark side.
The Vista features include:
SRWLOCK rwLock;
InitializeSRWLock(&rwLock);
AcquireSRWLockShared(&rwLock);
// ... shared operations ...
ReleaseSRWLockShared(&rwLock);
Buffer \* pBuffer = ...;
PCRITICAL\_SECTION pCsBufferLock = ...;
PCONDITION\_VARIABLE pCvBufferHasItem = ...;
// Producer code:
EnterCriticalSection(pCsBufferLock);
while (pBuffer->Count == 0) {
SleepConditionVariableCS(pCvBufferHasItem, pCsBufferLock, INFINITE);
}
// process item...
LeaveCriticalSection(pCsBufferLock);
// Consumer code:
EnterCriticalSection(pCsBufferLock);
pBuffer->Put(NewItem());
LeaveCriticalSection(pCsBufferLock);
WakeAllConditionVariable(pCvBufferHasItem);
More details on condition variables can be found on MSDN.
Lazy/one-time initialization. This allows you to write lazy allocation without fully understanding memory models and that sort of nonsense. The new APIs in kernel32, InitXXX, support both synchronous and asynchronous initialization. These have some amount of overhead for the initialization case due to the use of a callback, but in general this will be fast enough for most lazy initialization and much less error prone. Herb Sutter has proposed a similar construct for the VC++ language, and to be honest I wish we had this built-in to C# too. See the MSDN docs for an example and more details.
An overhauled thread pool API. The Windows kernel team has actually rewritten the thread pool from the ground up for this release. Their APIs now support creating multiple pools per process, waiting for queues to drain or a specific work item to complete, cancellation of work, cancellation of IO, and new cleanup functionality, including automatically releasing locks. It also has substantial performance improvements due to a large portion of the code residing in user-mode instead of kernel-mode. MSDN has a comparison between the old and new APIs.
A bunch of new InterlockedXXX variants.
Application deadlock detection. This is separate from the existing Driver Verifier ability to diagnose deadlocks in drivers. This capability integrates with all synchronization mechanisms, from CRITICAL_SECTION to SRWLOCK to Mutex, and keys off of any calls to XXXWaitForYYYObjectZZZ. Unfortunately, I think this is new to the latest Vista SDK, and thus there isn’t a lot of information available publicly. This could probably make a good future blog post if there’s interest.
Have fun with this stuff, of course. But be careful. Don’t poke an eye out.