lock::lock

完全に、指定した時間場合も、ロックを取得するには、オプションで待機する lock のオブジェクトを作成します。

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

パラメーター

  • _object
    ロックするオブジェクト。

  • _timeout
    ミリ秒のまたは TimeSpanにタイムアウト値。

例外

ロックの取得がタイムアウトの前に発生する ApplicationException をスローします。

解説

コンストラクターの最初の 3 とおりの形式は何も指定されていない場合)指定されたタイムアウト期間します (または Infinite 内の _object をロックしようとします。

コンストラクターの 4 番目の形式は _objectのロックを取得しません。lock_later は lock_when Enumのメンバーです。この場合、ロックを取得するに lock::acquirelock::try_acquire を使用します。

ロックは自動的にデストラクターを呼び出すと、解放されます。

_object に ReaderWriterLock は指定できません。この場合、コンパイラ エラーが発生します。

使用例

この例では、複数のスレッド間でクラスの単一インスタンスを使用します。クラスは、データへのアクセスが各スレッドに対して一貫していることを保証するために、自身のロックを使用します。メイン アプリケーション スレッドは定期的に確認するには、クラスの同じインスタンスのロックのワーカー スレッドでも、すべてのワーカー スレッド終了するまで待機が完了しているかどうかを使用します。

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

必要条件

ヘッダー ファイル <msclr \ lock.h>

名前空間 の msclr

参照

関連項目

lock::~lock

lock::acquire

lock::try_acquire

その他の技術情報

lock のメンバー