Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
MutexRW.h

MutexRW.h

Ratings: (0)|Views: 0 |Likes:
Published by Ashok Kumar

More info:

Published by: Ashok Kumar on Oct 19, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

10/19/2012

pdf

text

original

 
#ifndef __MUTEX_READERS_WRITERS#define __MUTEX_READERS_WRITERSclass CMutexRW{protected:HANDLEm_semReaders;HANDLEm_semWriters;intm_nReaders;public:CMutexRW() :m_semReaders(NULL),m_semWriters(NULL),m_nReaders(0){// initialize the Readers & Writers variablesm_semReaders= ::CreateSemaphore(NULL, 1, 1, NULL);m_semWriters= ::CreateSemaphore(NULL, 1, 1, NULL);m_nReaders= 0;if (m_semReaders == NULL || m_semWriters == NULL){LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language(LPTSTR) &lpMsgBuf,0,NULL);TRACE( "***** ERROR: CreateSemaphore: %s\n", (LPCTSTR)lpMsgBuf );LocalFree( lpMsgBuf );}};virtual ~CMutexRW(){if (m_semWriters)VERIFY( ::CloseHandle(m_semWriters) );m_semWriters = NULL;if (m_semReaders)VERIFY( ::CloseHandle(m_semReaders) );m_semReaders = NULL;}inline void Lock_DataRead(){DWORD dwEvent = WAIT_TIMEOUT;// P( semReaders )dwEvent = ::WaitForSingleObject( m_semReaders, INFINITE );ASSERT(dwEvent == WAIT_OBJECT_0);m_nReaders++;
 
if (m_nReaders == 1){// P( semWriters )dwEvent = ::WaitForSingleObject( m_semWriters, INFINITE);ASSERT(dwEvent == WAIT_OBJECT_0);}// V( semReaders )VERIFY( ::ReleaseSemaphore( m_semReaders, 1, NULL ) );};inline void Unlock_DataRead(){DWORD dwEvent = WAIT_TIMEOUT;// P( semReaders )dwEvent = ::WaitForSingleObject( m_semReaders, INFINITE );ASSERT(dwEvent == WAIT_OBJECT_0);m_nReaders--;if (m_nReaders == 0){// V( semWriters )VERIFY( ::ReleaseSemaphore(m_semWriters, 1, NULL) );}// V( semReaders )VERIFY( ::ReleaseSemaphore( m_semReaders, 1, NULL ) );};inline void Lock_DataWrite(){DWORD dwEvent = WAIT_TIMEOUT;// P( semWriters )dwEvent = ::WaitForSingleObject( m_semWriters, INFINITE );ASSERT(dwEvent == WAIT_OBJECT_0);}inline void Unlock_DataWrite(){// V( semWriters )VERIFY( ::ReleaseSemaphore(m_semWriters, 1, NULL) );};};class CReadLock{protected:CMutexRW*m_pMutexRW;boolm_bIsLocked;public:CReadLock(CMutexRW* pMutexRW, const bool bInitialLock = false) :m_pMutexRW(pMutexRW), m_bIsLocked(false){ASSERT(m_pMutexRW);if (bInitialLock){m_pMutexRW->Lock_DataRead();m_bIsLocked = true;}};
 
inline const bool& IsLocked() const{return m_bIsLocked;};inline void Lock(){ASSERT(m_bIsLocked == false);m_pMutexRW->Lock_DataRead();m_bIsLocked = true;};inline void Unlock(){ASSERT(m_bIsLocked);m_pMutexRW->Unlock_DataRead();m_bIsLocked = false;};virtual ~CReadLock(){if (m_bIsLocked){m_pMutexRW->Unlock_DataRead();}};};class CWriteLock{protected:CMutexRW*m_pMutexRW;boolm_bIsLocked;public:CWriteLock(CMutexRW* pMutexRW, const bool bInitialLock = false) :m_pMutexRW(pMutexRW), m_bIsLocked(false){ASSERT(m_pMutexRW);if (bInitialLock){m_pMutexRW->Lock_DataWrite();m_bIsLocked = true;}};inline const bool& IsLocked() const{return m_bIsLocked;};inline void Lock(){ASSERT(m_bIsLocked == false);m_pMutexRW->Lock_DataWrite();m_bIsLocked = true;};inline void Unlock(){ASSERT(m_bIsLocked);m_pMutexRW->Unlock_DataWrite();m_bIsLocked = false;};virtual ~CWriteLock(){if (m_bIsLocked){m_pMutexRW->Unlock_DataWrite();}};};

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->