Click here to Skip to main content
15,892,839 members
Articles / Programming Languages / Visual C++ 10.0

SmartObject Class (Objective-C like Memory Management) for C++ (A substitute for Smart Pointer maybe?)

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
22 Aug 2013MIT9 min read 35.1K   350   25  
This article explains Objective-C like C++ memory management class, SmartObject.
/*! 
Mutex for the EpLibrary

The MIT License (MIT)

Copyright (c) 2008-2013 Woong Gyu La <juhgiyo@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Mutex.h"



Mutex::Mutex(const TCHAR *mutexName, LPSECURITY_ATTRIBUTES lpsaAttributes) :BaseLock()
{
	m_isMutexAbandoned=false;
	m_lpsaAttributes=lpsaAttributes;
	m_mutex=CreateMutex(lpsaAttributes,FALSE,mutexName);
}

Mutex::Mutex(const Mutex& b)
{
	m_isMutexAbandoned=false;
	m_lpsaAttributes=b.m_lpsaAttributes;
	m_mutex=CreateMutex(m_lpsaAttributes,FALSE,NULL);
}

Mutex::~Mutex()
{

	CloseHandle(m_mutex);
}

bool Mutex::Lock()
{

	bool returnVal=true;
	unsigned long res=WaitForSingleObject(m_mutex,INFINITE);
	if(res==WAIT_ABANDONED)
	{
		m_isMutexAbandoned=true;
		returnVal=false;
	}
	return returnVal;
}

long Mutex::TryLock()
{
	long ret=0;
	unsigned long mutexStatus=WaitForSingleObject(m_mutex,0);
	if(mutexStatus== WAIT_OBJECT_0)
		ret=1;
	else if(mutexStatus==WAIT_ABANDONED)
		m_isMutexAbandoned=true;
	return ret;

}

long Mutex::TryLockFor(const unsigned int dwMilliSecond)
{
	long ret=0;
	unsigned long mutexStatus=WaitForSingleObject(m_mutex,dwMilliSecond);
	if(mutexStatus==WAIT_OBJECT_0)
		ret=1;
	else if(mutexStatus==WAIT_ABANDONED)
		m_isMutexAbandoned=true;
	return ret;

}
void Mutex::Unlock()
{
	ReleaseMutex(m_mutex);
}

bool Mutex::IsMutexAbandoned()
{
	return m_isMutexAbandoned;
}

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, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United States United States
Woong Gyu La had been working as a software developer for over 8 years.
His personal interests are improving his personal projects,

EpLibrary (Visual C++ Utility Library)
https://github.com/juhgiyo/EpLibrary[^]

EpOraLibrary (Oracle OCI Wrapper Library for Visual C++)
https://github.com/juhgiyo/EpOraLibrary[^]

EpServerEngine (Visual C++ WinSock Server/Client Engine)
https://github.com/juhgiyo/EpServerEngine[^]

And other projects can be found at
https://github.com/juhgiyo?tab=repositories[^]

Finally, my other articles can be found at
http://www.codeproject.com/Articles/juhgiyo#articles[^]

You can contact me at juhgiyo@gmail.com[^]

Comments and Discussions