ATL Critical-Section Classes

[Previous] [Next]

ATL critical-section classes wrap a Win32 critical-section synchronization object. These fairly simple classes rely on the standard InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection, and DeleteCriticalSection APIs. The following code shows the CComCriticalSection class from ATLBASE.H:

 class CComCriticalSection { public:     void Lock() {EnterCriticalSection(&m_sec);}     void Unlock() {LeaveCriticalSection(&m_sec);}     void Init() {InitializeCriticalSection(&m_sec);}     void Term() {DeleteCriticalSection(&m_sec);}     CRITICAL_SECTION m_sec; }; 

This implementation requires calling Init after construction. CComAutoCriticalSection calls Init in the constructor and Term in the destructor, freeing you from the need to remember to call them yourself. The price for this convenience is that you might need the CRT startup code if you use CComAutoCriticalSection in a static class member or global.

CComFakeCriticalSection doesn't wrap a Win32 critical section. In fact, all its methods are empty implementations. By declaring this type, ATL enables the reuse of the same semantics for both multithreaded and single-threaded object locking.

Although not seen in the inheritance tree in Figure 6-1, an ATL COM object is indirectly associated with a critical section through the threading-model class. You can also use CComCriticalSection and CComAutoCriticalSection as general-purpose critical sections outside the context of an object. In this chapter, we limit the discussion of critical sections to their application in the threading-model classes, which we examine next.



Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net