#include "SharedResource.h"
#include <windows.h>


SharedResource::SharedResource()
: _handle(0)
{
    _handle = CreateMutex(NULL, FALSE, NULL);
}

SharedResource::~SharedResource()
{
    ReleaseMutex(_handle);
	CloseHandle(_handle);
    _handle = 0;
}

void SharedResource::lock()
{
	WaitForSingleObject(_handle, INFINITE);  
}

void SharedResource::unlock()
{
	ReleaseMutex(_handle);
}

bool SharedResource::tryLock()
{
	unsigned long result = WaitForSingleObject(_handle, 0);

	if (result == WAIT_OBJECT_0)
		return true;
	else
		return false;
}