Skip to content

Latest commit

 

History

History
180 lines (154 loc) · 5.11 KB

File metadata and controls

180 lines (154 loc) · 5.11 KB
title lock::lock | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic reference
f1_keywords
lock::lock
lock.lock
msclr.lock.lock
msclr::lock::lock
dev_langs
C++
helpviewer_keywords
lock constructor
ms.assetid c9ad6c71-36ec-49c5-8ebd-f5c3a0cc94f0
caps.latest.revision 15
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

lock::lock

Constructs a lock object, optionally waiting to acquire the lock forever, for a specified amount of time, or not at all.

Syntax

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  
);  

Parameters

_object
The object to be locked.

_timeout
Timeout value in milliseconds or as a xref:System.TimeSpan.

Exceptions

Throws xref:System.ApplicationException if lock acquisition does not occur before timeout.

Remarks

The first three forms of the constructor attempt to acquire a lock on _object within the specified timeout period (or xref:System.Threading.Timeout.Infinite if none is specified).

The fourth form of the constructor does not acquire a lock on _object. lock_later is a member of the lock_when Enum. Use lock::acquire or lock::try_acquire to acquire the lock in this case.

The lock will automatically be released when the destructor is called.

_object cannot be xref:System.Threading.ReaderWriterLock. If it is, a compiler error will result.

Example

This example uses a single instance of a class across multiple threads. The class uses a lock on itself to ensure that accesses to its internal data are consistent for each thread. The main application thread uses a lock on the same instance of the class to periodically check to see if any worker threads still exist, and waits to exit until all worker threads have completed their tasks.

// 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.  

Requirements

Header file <msclr\lock.h>

Namespace msclr

See Also

lock Members
lock::~lock
lock::acquire
lock::try_acquire