Click here to Skip to main content
11,639,861 members (61,211 online)
Click here to Skip to main content
Add your own
alternative version

STL WebServer

, 8 May 2000 73.7K 2.2K 47
A set of classes written in STL that implement a web server
//////////////////////////////////////////////////////////////////////// 
// 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.

License

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

Share

About the Author

David Hubbard
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 9 May 2000
Article Copyright 2000 by David Hubbard
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid