Lock in MC++

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Jesse McGrew said:
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();
}
 
Back
Top