次の方法で共有


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::acquire または lock::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
         }
      }
   }
}

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.

必要条件

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

名前空間 msclr

参照

概念

lock のメンバ

lock::~lock

lock::acquire

lock::try_acquire