Welcome to CCriticalSeciontEx, a drop in replacement for the MFC class
CCriticalSection which provides a MFC critical section with timeouts and spint
counts. The code is based
on the code presented in Chapter Ten of the book "Programming Applications for Microsoft
Windows" by Jeffrey Richter.
Features
- Simple and clean C++ interface.
- The code is Unicode enabled and build configurations are provided.
- The classes integrate closely with MFC. i.e. instead of the CResGuard class
we use the class "CCriticalSectionEx". This class implements timeouts and spin
counts compared to the standard MFC CCriticalSection class.
The enclosed zip file contains the source code
and a simple VC2017 test program to exercise all of the classes.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Updates
3 May 2020
- Please note that the built-in MFC class CCriticalSection has
supported spin counts for some releases now. As such I have discontinued development
of this code and it is considered obsolete.
v1.06 (12 March 2020)
- Updated copyright details.
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.05 (8 December 2018)
- Updated copyright details.
- Removed deprecated TSafe.h from download
v1.04 (17 January 2016)
- Updated copyright details.
- Verified the code compiles cleanly in VC 2015
- Added SAL annotations to all the code
- The CCriticalSectionEx class has been moved to its own module of CriticalSectionEx.h
- You should consider the "CThreadSafe" and "CThreadSafeScalar" classes deprecated
as there is now equivalent support in the STL C++ runtime via the "atomic" header
file.
v1.03 (5 April 2015)
- Updated copyright details.
- Updated the sample app and code to clean compile in VC 2010 and later.
- The code now statically references InitializeCriticalSectionAndSpinCount,
TryEnterCriticalSection & SetCriticalSectionSpinCount rather than by using
GetProcAddress.
- Removed the SpinCountsAvailable, TimeoutsAvailable & NumberOfProcessors
methods as we are not using GetProcAddress anymore.
- Removed the m_bLocked member variable.
v1.02 (11 January 2009)
- Updated copyright details
- Removed VC 6 style AppWizard comments from the code
- The code has now been updated to support VC 2005 or later only. It seems
that even though ATL's version of CCriticalSection now supports spin locks the
MFC version of CCriticalSection still does not support them, hence still the
need for CCriticalSectionEx.
- Fixed a bug in CCriticalSectionEx::Lock where it handled the wrong SEH type
being thrown. The code now only handles the STATUS_NO_MEMORY exception
- Code now compiles cleanly using Code Analysis (/analyze)
- Removed the bPreallocateEvent parameter from the CCriticalSectionEx::Init
and CCriticalSectionEx's overridden constructor.
- Removed the call to Unlock in the CCriticalSectionEx destructor. This is
to be consistent with MFC's CCriticalSection class.
- CCriticalSectionEx::Lock now throws a CMemoryException* exception. This
is to be consistent with MFC's CCriticalSection class.
- The m_bLocked member variable of CCriticalSectionEx is now a bool instead
of a BOOL. The IsLocked method has also been updated to return a bool.
v1.01 (22 December 2006)
- Updated copyright details
- Optimized CThreadSafe constructor code
- Code now uses newer C++ style casts instead of C style casts.
- Updated the code to clean compile on VC 2005
- Made CCriticalSectionEx::Lock and Unlock non virtual
- Optimized _CRITICAL_SECTION_DATA constructor code
- CCriticalSectionEx constructors now throws an exception using AfxThrowMemoryException
instead of AfxThrowResourceException
- Removed unnecessary CThreadSafe destructor
- Removed unnecessary CThreadSafeScalar destructor
- Added postfix operator support to CThreadSafeScalar
- Addition of a operator TYPE*() in CThreadSafe
- m_Value member variable of CThreadSafe is now public
- Addition of a CCRITICALSECTIONEX_EXT_CLASS preprocessor macro to allow the
classes to be more easily added to an extension dll
v1.0 (4 December 2002)