#include <Windows.h>
class Mutex
{
public:
Mutex()
{
m_mutex = CreateMutex(NULL, FALSE, NULL);
}
~Mutex()
{
CloseHandle(m_mutex);
}
void lock()
{
WaitForSingleObject(m_mutex, INFINITE);
}
void unlock()
{
ReleaseMutex(m_mutex);
}
private:
HANDLE m_mutex;
};
class MutexLocker
{
public:
explicit MutexLocker(Mutex& mtx) : m_mutex(mtx)
{
m_mutex.lock();
}
~MutexLocker()
{
m_mutex.unlock();
}
private:
Mutex& m_mutex;
};