CMutex :: Lock vs CSingleLock :: Lock

I was used to support some outdated code, and I see some things that make me confuse me. In some sections of the code, I see that the class instance uses the CMutex instance to synchronize the execution of the method. For instance,

class CClassA : public CObject
{
public:
   void DoSomething();

private:
   CMutex m_mutex;
}

void CClassA::DoSomething()
{
   m_mutex.Lock();

   //...logic...

   m_mutex.Unlock();
}

Elsewhere in the same project, I found that the code uses CSingleLock

class CClassB : public CObject
{
public:
   void DoSomething();

private:
   CCriticalSection m_crit;
}

void CClassB::DoSomething()
{
   CSingleLock lock(&m_crit);
   lock.Lock();

   //...logic...

   lock.Unlock();
}

MSDN , , CClassB , , , CClassA. , , CSingleLock RAII, , . / ?

+3
2

, .

RAII, , . , , -, ...

void DoSomething()
{
  // construction acquires lock on mutex
  boost::scoped_lock lock(&aBoostMutex);

  // ...

} // end scope - object is destroyed and lock is released

, CMutex, CCritalSection, CSemaphore CEvent, , , , boost. :

  • - , , .
  • CSingleLock, .

, , MFC API Windows - boost ++ 0x, std::mutex, , - .

+1

/ . ( ). , , , , , .

+1

All Articles