Aracılığıyla paylaş


lock Sınıfı

Bu sınıf, birkaç iş parçacığından bir nesneye erişimi eşitlemek için kilit almayı otomatikleştirir. Oluşturulduğunda kilidi alır ve yok edildiğinde kilidi serbest bırakır.

Sözdizimi

ref class lock;

Açıklamalar

lock yalnızca CLR nesneleri için kullanılabilir ve yalnızca CLR kodunda kullanılabilir.

Dahili olarak, kilit sınıfı erişimi eşitlemek için kullanır Monitor . Daha fazla bilgi için başvuruda bulunan makaleye bakın.

Üyeler

Ortak oluşturucular

Veri Akışı Adı Açıklama
lock::lock lock İsteğe bağlı olarak kilidi sonsuza kadar, belirli bir süre boyunca almayı bekleyen veya hiç beklemeyen bir nesne oluşturur.
lock::~lock Bir lock nesneyi yok eder.

Genel yöntemler

Veri Akışı Adı Açıklama
lock::acquire Bir nesne üzerinde, isteğe bağlı olarak kilidin sonsuza kadar, belirli bir süre boyunca veya hiç alınmaması için bekleyen bir kilit alır.
lock::is_locked Bir kilidin tutulup tutulmadığını gösterir.
lock::release Kilidi serbest bırakır.
lock::try_acquire Belirli bir süre için bekleyen ve bir özel durum oluşturmak yerine alma işleminin başarısını bildirmek için döndüren bool bir nesne üzerinde kilit alır.

Genel işleçler

Veri Akışı Adı Açıklama
lock::operator bool Koşullu ifadede kullanmak lock için işleç.
lock::operator== Eşitlik işleci.
lock::operator!= Eşitsizlik işleci.

Gereksinimler

Üst bilgi dosyası<msclr\lock.h>

Ad alanı msclr

lock::lock

lock İsteğe bağlı olarak kilidi sonsuza kadar, belirli bir süre boyunca almayı bekleyen veya hiç beklemeyen bir nesne oluşturur.

template<class T> lock(
   T ^ _object
);
template<class T> lock(
   T ^ _object,
   int _timeout
);
template<class T> lock(
   T ^ _object,
   System::TimeSpan _timeout
);
template<class T> lock(
   T ^ _object,
   lock_later
);

Parametreler

_nesne
Kilitlenecek nesne.

_Zaman aşımı
Milisaniye cinsinden veya olarak TimeSpanzaman aşımı değeri.

Özel durumlar

ApplicationException Zaman aşımından önce kilit alımı gerçekleşmezse atar.

Açıklamalar

Oluşturucunun ilk üç biçimi, belirtilen zaman aşımı süresi içinde (veya Infinite belirtilmemişse) bir kilit _object almaya çalışır.

Oluşturucunun dördüncü biçimi üzerinde _objectbir kilit almaz. lock_laterlock_when sabit listesi üyesidir. Bu durumda kilidi almak için lock::acquire veya lock::try_acquire kullanın.

Yıkıcı çağrıldığında kilit otomatik olarak serbest bırakılır.

_object olamaz ReaderWriterLock. Bu durumda bir derleyici hatası oluşur.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ardından ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_lock.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      if (l.try_acquire(50)) { // try to acquire lock, don't throw an exception if can't
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::~lock

Bir lock nesneyi yok eder.

~lock();

Açıklamalar

Yıkıcı lock::release öğesini çağırır.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ardından ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_dtor.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      if (l.try_acquire(50)) { // try to acquire lock, don't throw an exception if can't
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::acquire

Bir nesne üzerinde, isteğe bağlı olarak kilidin sonsuza kadar, belirli bir süre boyunca veya hiç alınmaması için bekleyen bir kilit alır.

void acquire();
void acquire(
   int _timeout
);
void acquire(
   System::TimeSpan _timeout
);

Parametreler

_Zaman aşımı
Milisaniye cinsinden veya olarak TimeSpanzaman aşımı değeri.

Özel durumlar

ApplicationException Zaman aşımından önce kilit alımı gerçekleşmezse atar.

Açıklamalar

Zaman aşımı değeri sağlanmazsa, varsayılan zaman aşımı olur Infinite.

Bir kilit zaten alınmışsa, bu işlev hiçbir şey yapmaz.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ardından ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_acquire.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      if (l.try_acquire(50)) { // try to acquire lock, don't throw an exception if can't
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::is_locked

Bir kilidin tutulup tutulmadığını gösterir.

bool is_locked();

Dönüş değeri

true kilit tutulduysa, false aksi takdirde.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır ve tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_is_locked.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      l.try_acquire(50); // try to acquire lock, don't throw an exception if can't
      if (l.is_locked()) { // check if we got the lock
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::operator bool

Koşullu ifadede kullanmak lock için işleç.

operator bool();

Dönüş değeri

true kilit tutulduysa, false aksi takdirde.

Açıklamalar

Bu işleç aslında bir tamsayıyı türüne _detail_class::_safe_bool dönüştürülemediğinden daha bool güvenli olan öğesine dönüştürür.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_op_bool.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      l.try_acquire(50); // try to acquire lock, don't throw an exception if can't
      if (l) { // use bool operator to check for lock
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::release

Kilidi serbest bırakır.

void release();

Açıklamalar

Kilit tutulmuyorsa hiçbir release şey yapmaz.

Bu işlevi açıkça çağırmanız gerekmez. Bir lock nesne kapsamın dışına çıktığında, yok edicisi öğesini çağırır release.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ardından ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_release.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      if (l.try_acquire(50)) { // try to acquire lock, don't throw an exception if can't
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::try_acquire

Belirli bir süre için bekleyen ve bir özel durum oluşturmak yerine alma işleminin başarısını bildirmek için döndüren bool bir nesne üzerinde kilit alır.

bool try_acquire(
   int _timeout_ms
);
bool try_acquire(
   System::TimeSpan _timeout
);

Parametreler

_Zaman aşımı
Milisaniye cinsinden veya olarak TimeSpanzaman aşımı değeri.

Dönüş değeri

true kilit alındıysa, false aksi takdirde.

Açıklamalar

Bir kilit zaten alınmışsa, bu işlev hiçbir şey yapmaz.

Örnek

Bu örnek, birkaç iş parçacığında bir sınıfın tek bir örneğini kullanır. sınıfı, iç verilerine erişimin her iş parçacığı için tutarlı olduğundan emin olmak için kendi üzerinde bir kilit kullanır. Ana uygulama iş parçacığı, herhangi bir çalışan iş parçacığının hala var olup olmadığını düzenli aralıklarla denetlemek için sınıfın aynı örneğinde bir kilit kullanır. Ardından ana uygulama, tüm çalışan iş parçacıkları görevlerini tamamlayana kadar çıkmak için bekler.

// msl_lock_try_acquire.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

ref class CounterClass {
private:
   int Counter;

public:
   property int ThreadCount;

   // function called by multiple threads, use lock to keep Counter consistent
   // for each thread
   void UseCounter() {
      try {
         lock l(this); // wait infinitely

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         for (int i = 0; i < 10; i++) {
            Counter++;
            Thread::Sleep(10);
         }

         Console::WriteLine("In thread {0}, Counter = {1}", Thread::CurrentThread->ManagedThreadId,
            Counter);

         Counter = 0;
         // lock is automatically released when it goes out of scope and its destructor is called
      }
      catch (...) {
         Console::WriteLine("Couldn't acquire lock!");
      }

      ThreadCount--;
   }
};

int main() {
   // create a few threads to contend for access to the shared data
   CounterClass^ cc = gcnew CounterClass;
   array<Thread^>^ tarr = gcnew array<Thread^>(5);
   ThreadStart^ startDelegate = gcnew ThreadStart(cc, &CounterClass::UseCounter);
   for (int i = 0; i < tarr->Length; i++) {
      tarr[i] = gcnew Thread(startDelegate);
      cc->ThreadCount++;
      tarr[i]->Start();
   }

   // keep our main thread alive until all worker threads have completed
   lock l(cc, lock_later); // don't lock now, just create the object
   while (true) {
      if (l.try_acquire(50)) { // try to acquire lock, don't throw an exception if can't
         if (0 == cc->ThreadCount) {
            Console::WriteLine("All threads completed.");
            break; // all threads are gone, exit while
         }
         else {
            Console::WriteLine("{0} threads exist, continue waiting...", cc->ThreadCount);
            l.release(); // some threads exist, let them do their work
         }
      }
   }
}
In thread 3, Counter = 0
In thread 3, Counter = 10
In thread 5, Counter = 0
In thread 5, Counter = 10
In thread 7, Counter = 0
In thread 7, Counter = 10
In thread 4, Counter = 0
In thread 4, Counter = 10
In thread 6, Counter = 0
In thread 6, Counter = 10
All threads completed.

lock::operator==

Eşitlik işleci.

template<class T> bool operator==(
   T t
);

Parametreler

t
Eşitlik için karşılaştıracak nesne.

Dönüş değeri

Kilidin nesnesiyle aynıysa t döndürürtrue, false aksi takdirde.

Örnek

// msl_lock_op_eq.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

int main () {
   Object^ o1 = gcnew Object;
   lock l1(o1);
   if (l1 == o1) {
      Console::WriteLine("Equal!");
   }
}
Equal!

lock::operator!=

Eşitsizlik işleci.

template<class T> bool operator!=(
   T t
);

Parametreler

t
Eşitsizlik için karşılaştıracak nesne.

Dönüş değeri

Kilidin nesnesinden farklıysa t döndürürtrue, false aksi takdirde.

Örnek

// msl_lock_op_ineq.cpp
// compile with: /clr
#include <msclr/lock.h>

using namespace System;
using namespace System::Threading;
using namespace msclr;

int main () {
   Object^ o1 = gcnew Object;
   Object^ o2 = gcnew Object;
   lock l1(o1);
   if (l1 != o2) {
      Console::WriteLine("Inequal!");
   }
}
Inequal!