Partager via


Comment : implémenter le mot clé C# lock en C++

Mise à jour : novembre 2007

Cette rubrique indique comment implémenter le mot clé C# lock en Visual C++. Pour plus d'informations, consultez lock, instruction (Référence C#).

Vous pouvez également utiliser la classe lock dans la bibliothèque de prise en charge C++. Pour plus d'informations, consultez Synchronization (lock Class).

Exemple

// CS_lock_in_CPP.cpp
// compile with: /clr /c
using namespace System::Threading;
ref class Lock {
   Object^ m_pObject;
   Lock % operator=( Lock const % );
   Lock( Lock const % );
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
}

Voir aussi

Autres ressources

Interopérabilité avec d'autres langages .NET en C++