A C ++ Class Wrapper To Simplify The Use of crringal

zhaozj2021-02-17  73

................... ..

Static CthreadMutext G_locksomething;

// a Function That Want to Access Some Thing

...

Cguard Guard (Locksomething);

// A c class wrapper to simplify the use of CRITICAL_SECTIONclass CThreadMutex {public: CThreadMutex () {:: InitializeCriticalSection (& m_cs);} ~ CThreadMutex () {:: DeleteCriticalSection (& m_cs);} void Enter () const {:: EnterCriticalSection ((Lpcritical_section) & m_cs);} void Leave () const {: LeavecriticalSection ((lpcritical_section) & m_cs);

#if (_WIN32_WINNT> = 0x0400) BOOL TRYENTER () const {return :: tryentercriticalsection ((lpcritical_section) & m_cs);} #ENDIF / * _WIN32_WINNT> = 0x0400 * /

Private: critical_section m_cs;

Hidden_copy (cthreadmutex); MEM_LEAK_DETECT;

// Gurad class that use stack to autoly perform lock and unloack actiontemplate class CGuard {public: CGuard (const MUTEX & mutex): m_oMutex (mutex) {m_oMutex.Enter ();} ~ CGuard () {m_oMutex.Leave ();} Private: const mutex & m_omutex; cguard (const cguard &); cguard & operator = (const cguard);

转载请注明原文地址:https://www.9cbs.com/read-30586.html

New Post(0)