CAutoLock Class

CAutoLock class hierarchy

The CAutoLock class holds a critical section for the scope of a block or function. The constructor locks the critical section and the destructor unlocks it. The object passed to the CAutoLock constructor must be derived from the CCritSec class. Thus, by declaring a CAutoLock object as a local variable in a function, as shown in the following code sample, a critical section can be locked without the danger of forgetting to unlock it in some of the code paths: the destructor ensures that upon exit from the function (or the scope of the declaration), the critical section will be unlocked. Member functions in this class are not designed for overriding.

/* Typical usage ensuring object is always unlocked correctly */

HRESULT MyFunc(IMediaSample *pSample)
{
   CAutoLock cObjectLock(m_pMyLock);

   /* Ignore samples passed when inactive */

   if (!m_bActive) {
      return NOERROR;
   }

   /* Add the sample to the pending queue */

   HRESULT hr = m_PendingList.AddTail(pSample);
   if (FAILED(hr)) {
      pSample->Release();
      return hr;
   }
   return NOERROR;
}

Protected Data Members

m_pLock Critical section for this lock.

Member Functions

CAutoLock Takes a pointer to a critical section object and locks it.

CAutoLock::CAutoLock

CAutoLock Class

Takes a pointer to a critical section object and locks it.

Syntax

CAutoLock(

  CCritSec *plock
  );

Parameters

plock
Pointer to a critical section object.

Return Value

No return value.

Remarks

The critical section is unlocked when the CAutoLock object is destroyed.


Top of Page Top of Page
© 2000 Microsoft and/or its suppliers. All rights reserved. Terms of Use.