> What is the equivalent of Lock keyword in MC++
Monitor::Enter() and Leave() with a __finally block.
Jesse
smk - 05 Jun 2004 12:36 GMT
Thanks. It work
----- Jesse McGrew wrote: ----
SMK wrote
> What is the equivalent of Lock keyword in MC+
Monitor::Enter() and Leave() with a __finally block
Jess
Tomas Restrepo \(MVP\) - 05 Jun 2004 16:01 GMT
> > What is the equivalent of Lock keyword in MC++
>
> Monitor::Enter() and Leave() with a __finally block.
A nice way of wrapping it might be something similar to this:
#using <mscorlib.dll>
#include <vcclr.h>
using namespace System;
template <typename T>
struct sync_lock
{
private: gcroot<T> v_t;
public:
sync_lock(T t) {
v_t = t;
System::Threading::Monitor::Enter(v_t);
}
~sync_lock() {
System::Threading::Monitor::Exit(v_t);;
}
};
__gc class MyClass
{
private: int m_v;
public:
void DoIt()
{
sync_lock<MyClass*> lock(this);
m_v += 5;
}
};
int main()
{
MyClass* c = new MyClass();
c->DoIt();
}

Signature
Tomas Restrepo
tomasr@mvps.org