|
////////////////////////////////////////////////////////////////////////
// Lock.h: interface for the Lock class.
//
#if !defined(Lock_H)
#define Lock_H
#include <string>
using namespace std;
/////////////////////////////////////////////////
// Lock
class Lock
{
public:
HANDLE m_hMutex; // used to lock/unlock object access
bool m_attached; // shows attached to existing lock
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Lock( string & lockName ) :
m_hMutex(NULL),
m_attached(false)
{
createLock(lockName);
}
Lock() :
m_hMutex(NULL),
m_attached(false)
{
createLock();
}
Lock( HANDLE hMutex ) :
m_hMutex(NULL),
m_attached(false)
{
if ( hMutex != NULL )
{
m_hMutex = hMutex;
m_attached = true;
}
}
virtual ~Lock()
{
release();
}
void release ()
{
destroyLock();
}
/////////////////////////////////////////////////////////////
// access object fxns
bool lock ()
{
if ( m_hMutex == NULL )
return false;
WaitForSingleObject( m_hMutex, INFINITE );
return true;
}
void unlock ()
{
ReleaseMutex(m_hMutex);
}
bool createLock ( string & lockName )
{
// return object mutex
m_hMutex = ::CreateMutex( NULL, false, lockName.c_str() );
if ( m_hMutex == 0 )
return false;
return true;
}
bool createLock ()
{
// return object mutex
m_hMutex = ::CreateMutex( NULL, false, 0 );
if ( m_hMutex == 0 )
return false;
return true;
}
static bool createLock ( HANDLE & hMutex )
{
// return object mutex
hMutex = ::CreateMutex( NULL, false, 0 );
if ( hMutex == 0 )
return false;
return true;
}
void destroyLock ()
{
if ( !m_attached )
{
if ( m_hMutex != NULL )
{
::CloseHandle( m_hMutex );
}
}
m_attached = false;
m_hMutex = NULL;
}
};
#endif
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.