Procedura: implementare la parola chiave lock di C# (C++/CLI)

In questo argomento viene illustrato come implementare la parola chiave lock di C# in Visual C++. Per ulteriori informazioni, vedere Istruzione lock (Riferimenti per C#).

È possibile utilizzare la classe lock contenuta nella libreria di supporto C++. Per ulteriori informazioni, vedere Synchronization (lock Class).

Esempio

// CS_lock_in_CPP.cpp
// compile with: /clr
using namespace System::Threading;
ref class Lock {
   Object^ m_pObject;
public:
   Lock( Object ^ pObject ) : m_pObject( pObject ) {
      Monitor::Enter( m_pObject );
   }
   ~Lock() {
      Monitor::Exit( m_pObject );
   }
};

ref struct LockHelper {
   void DoSomething();
};

void LockHelper::DoSomething() {
   // Note: Reference type with stack allocation semantics to provide 
   // deterministic finalization

   Lock lock( this );   
   // LockHelper instance is locked
}

int main()
{
   LockHelper lockHelper;
   lockHelper.DoSomething();
   return 0;
}

Vedere anche

Altre risorse

Interoperabilità con altri linguaggi .NET (C++/CLI)