G
Guest
In the prior C++ world we used to have locks that we would declare on the
stack such that as the stack was unwound the critical section would be
released. I am wondering how this works with managed C++. Here is a class
that I wrote to wrap a monitor for an object around. I would appreciate any
comments about this I have looked at it in the debugger and it appears to
work. This isn't a class that you would use gcnew on, but declare on the
stack: ESLock myLock(mySharedObject);
using namespace System;
using namespace System::Threading;
public ref class ESLock : IDisposable
{
public:
ESLock(Object ^ obj) : m_bLocked(false), m_obj(obj)
{
Monitor::Enter(obj);
m_bLocked = true;
}
~ESLock()
{
if(m_bLocked) Unlock();
}
void Unlock()
{
if(!m_bLocked) return;
Monitor::Exit(m_obj);
m_bLocked = false;
}
protected:
Object ^m_obj;
bool m_bLocked;
};
stack such that as the stack was unwound the critical section would be
released. I am wondering how this works with managed C++. Here is a class
that I wrote to wrap a monitor for an object around. I would appreciate any
comments about this I have looked at it in the debugger and it appears to
work. This isn't a class that you would use gcnew on, but declare on the
stack: ESLock myLock(mySharedObject);
using namespace System;
using namespace System::Threading;
public ref class ESLock : IDisposable
{
public:
ESLock(Object ^ obj) : m_bLocked(false), m_obj(obj)
{
Monitor::Enter(obj);
m_bLocked = true;
}
~ESLock()
{
if(m_bLocked) Unlock();
}
void Unlock()
{
if(!m_bLocked) return;
Monitor::Exit(m_obj);
m_bLocked = false;
}
protected:
Object ^m_obj;
bool m_bLocked;
};