英語で読む

次の方法で共有


Monitor.Enter メソッド

定義

指定したオブジェクトの排他ロックを取得します。

オーバーロード

Enter(Object)

指定したオブジェクトの排他ロックを取得します。

Enter(Object, Boolean)

指定したオブジェクトの排他ロックを取得し、ロックが取得されたかどうかを示す値をアトミックに設定します。

Enter(Object)

ソース:
Monitor.cs
ソース:
Monitor.cs
ソース:
Monitor.cs

指定したオブジェクトの排他ロックを取得します。

C#
public static void Enter (object obj);

パラメーター

obj
Object

モニター ロックを取得する対象となるオブジェクト。

例外

obj パラメーターが null です。

Enter メソッドの使用方法を次の例に示します。

C#
using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;

class SafeQueue<T>
{
   // A queue that is protected by Monitor.
   private Queue<T> m_inputQueue = new Queue<T>();

   // Lock the queue and add an element.
   public void Enqueue(T qValue)
   {
      // Request the lock, and block until it is obtained.
      Monitor.Enter(m_inputQueue);
      try
      {
         // When the lock is obtained, add an element.
         m_inputQueue.Enqueue(qValue);
      }
      finally
      {
         // Ensure that the lock is released.
         Monitor.Exit(m_inputQueue);
      }
   }

   // Try to add an element to the queue: Add the element to the queue
   // only if the lock is immediately available.
   public bool TryEnqueue(T qValue)
   {
      // Request the lock.
      if (Monitor.TryEnter(m_inputQueue))
      {
         try
         {
            m_inputQueue.Enqueue(qValue);
         }
         finally
         {
            // Ensure that the lock is released.
            Monitor.Exit(m_inputQueue);
         }
         return true;
      }
      else
      {
         return false;
      }
   }

   // Try to add an element to the queue: Add the element to the queue
   // only if the lock becomes available during the specified time
   // interval.
   public bool TryEnqueue(T qValue, int waitTime)
   {
      // Request the lock.
      if (Monitor.TryEnter(m_inputQueue, waitTime))
      {
         try
         {
            m_inputQueue.Enqueue(qValue);
         }
         finally
         {
            // Ensure that the lock is released.
            Monitor.Exit(m_inputQueue);
         }
         return true;
      }
      else
      {
         return false;
      }
   }

   // Lock the queue and dequeue an element.
   public T Dequeue()
   {
      T retval;

      // Request the lock, and block until it is obtained.
      Monitor.Enter(m_inputQueue);
      try
      {
         // When the lock is obtained, dequeue an element.
         retval = m_inputQueue.Dequeue();
      }
      finally
      {
         // Ensure that the lock is released.
         Monitor.Exit(m_inputQueue);
      }

      return retval;
   }

   // Delete all elements that equal the given object.
   public int Remove(T qValue)
   {
      int removedCt = 0;

      // Wait until the lock is available and lock the queue.
      Monitor.Enter(m_inputQueue);
      try
      {
         int counter = m_inputQueue.Count;
         while (counter > 0)
            // Check each element.
         {
            T elem = m_inputQueue.Dequeue();
            if (!elem.Equals(qValue))
            {
               m_inputQueue.Enqueue(elem);
            }
            else
            {
               // Keep a count of items removed.
               removedCt += 1;
            }
            counter = counter - 1;
         }
      }
      finally
      {
         // Ensure that the lock is released.
         Monitor.Exit(m_inputQueue);
      }

      return removedCt;
   }

   // Print all queue elements.
   public string PrintAllElements()
   {
      StringBuilder output = new StringBuilder();

      // Lock the queue.
      Monitor.Enter(m_inputQueue);
      try
      {
         foreach( T elem in m_inputQueue )
         {
            // Print the next element.
            output.AppendLine(elem.ToString());
         }
      }
      finally
      {
         // Ensure that the lock is released.
         Monitor.Exit(m_inputQueue);
      }

      return output.ToString();
   }
}

public class Example
{
   private static SafeQueue<int> q = new SafeQueue<int>();
   private static int threadsRunning = 0;
   private static int[][] results = new int[3][];

   static void Main()
   {
      Console.WriteLine("Working...");

      for(int i = 0; i < 3; i++)
      {
         Thread t = new Thread(ThreadProc);
         t.Start(i);
         Interlocked.Increment(ref threadsRunning);
      }
   }

   private static void ThreadProc(object state)
   {
      DateTime finish = DateTime.Now.AddSeconds(10);
      Random rand = new Random();
      int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
      int threadNum = (int) state;

      while (DateTime.Now < finish)

      {
         int what = rand.Next(250);
         int how = rand.Next(100);

         if (how < 16)
         {
            q.Enqueue(what);
            result[(int)ThreadResultIndex.EnqueueCt] += 1;
         }
         else if (how < 32)
         {
            if (q.TryEnqueue(what))
            {
               result[(int)ThreadResultIndex.TryEnqueueSucceedCt] += 1;
            }
            else
            {
               result[(int)ThreadResultIndex.TryEnqueueFailCt] += 1;
            }
         }
         else if (how < 48)
         {
            // Even a very small wait significantly increases the success
            // rate of the conditional enqueue operation.
            if (q.TryEnqueue(what, 10))
            {
               result[(int)ThreadResultIndex.TryEnqueueWaitSucceedCt] += 1;
            }
            else
            {
               result[(int)ThreadResultIndex.TryEnqueueWaitFailCt] += 1;
            }
         }
         else if (how < 96)
         {
            result[(int)ThreadResultIndex.DequeueCt] += 1;
            try
            {
               q.Dequeue();
            }
            catch
            {
               result[(int)ThreadResultIndex.DequeueExCt] += 1;
            }
         }
         else
         {
            result[(int)ThreadResultIndex.RemoveCt] += 1;
            result[(int)ThreadResultIndex.RemovedCt] += q.Remove(what);
         }
      }

      results[threadNum] = result;

      if (0 == Interlocked.Decrement(ref threadsRunning))
      {
         StringBuilder sb = new StringBuilder(
            "                               Thread 1 Thread 2 Thread 3    Total\n");

         for(int row = 0; row < 9; row++)
         {
            int total = 0;
            sb.Append(titles[row]);

            for(int col = 0; col < 3; col++)
            {
               sb.Append(String.Format("{0,9}", results[col][row]));
               total += results[col][row];
            }

            sb.AppendLine(String.Format("{0,9}", total));
         }

         Console.WriteLine(sb.ToString());
      }
   }

   private static string[] titles = {
      "Enqueue                       ",
      "TryEnqueue succeeded          ",
      "TryEnqueue failed             ",
      "TryEnqueue(T, wait) succeeded ",
      "TryEnqueue(T, wait) failed    ",
      "Dequeue attempts              ",
      "Dequeue exceptions            ",
      "Remove operations             ",
      "Queue elements removed        "};

   private enum ThreadResultIndex
   {
      EnqueueCt,
      TryEnqueueSucceedCt,
      TryEnqueueFailCt,
      TryEnqueueWaitSucceedCt,
      TryEnqueueWaitFailCt,
      DequeueCt,
      DequeueExCt,
      RemoveCt,
      RemovedCt
   };
}

/* This example produces output similar to the following:

Working...
                               Thread 1 Thread 2 Thread 3    Total
Enqueue                          277382   515209   308464  1101055
TryEnqueue succeeded             276873   514621   308099  1099593
TryEnqueue failed                   109      181      134      424
TryEnqueue(T, wait) succeeded    276913   514434   307607  1098954
TryEnqueue(T, wait) failed            2        0        0        2
Dequeue attempts                 830980  1544081   924164  3299225
Dequeue exceptions                12102    21589    13539    47230
Remove operations                 69550   129479    77351   276380
Queue elements removed            11957    22572    13043    47572
 */

注釈

パラメーターとして渡された オブジェクトの をMonitor取得するには、 を使用Enterします。 別のスレッドが オブジェクトで を Enter 実行したが、対応する Exitをまだ実行していない場合、現在のスレッドは、他のスレッドが オブジェクトを解放するまでブロックします。 同じスレッドがブロックせずに複数回呼び出 Enter されることは有効ですが、オブジェクトで待機している他のスレッドがブロックを解除する前に、同じ数の Exit 呼び出しを呼び出す必要があります。

を使用して Monitor 、値型ではなくオブジェクト (つまり参照型) をロックします。 値型変数を に Enter渡すと、オブジェクトとしてボックス化されます。 同じ変数を再度 に Enter 渡すと、別のオブジェクトとしてボックス化され、スレッドはブロックされません。 この場合、保護していると Monitor 思われるコードは保護されません。 さらに、 変数を に Exit渡すと、別のオブジェクトが作成されます。 に渡されるオブジェクトは にExit渡されるオブジェクトとは異なるため、 は を MonitorEnterスローしますSynchronizationLockException。 詳細については、概念に関するトピック 「モニター」を参照してください。

Interrupt は、オブジェクトに を入力 Monitor するのを待機しているスレッドを中断できます。 ThreadInterruptedExceptionがスローされます。

C# を使用して、 try. finallyブロック (Try...Finally Visual Basic の場合) を使用してモニターを解放するか、C# lock ステートメント (SyncLock Visual Basic のステートメント) を使用します。これにより、 メソッドと Exit メソッドが ... でtryラップEnterされます。 finally ブロック。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Enter(Object, Boolean)

ソース:
Monitor.CoreCLR.cs
ソース:
Monitor.CoreCLR.cs
ソース:
Monitor.CoreCLR.cs

指定したオブジェクトの排他ロックを取得し、ロックが取得されたかどうかを示す値をアトミックに設定します。

C#
public static void Enter (object obj, ref bool lockTaken);

パラメーター

obj
Object

待機を行うオブジェクト。

lockTaken
Boolean

ロックを取得しようとした結果で、参照渡しです。 入力は false でなければなりません。 ロックが取得された場合、出力は true になります。それ以外の場合、出力は false です。 ロックを取得しようとしている間に例外が発生した場合でも、出力は設定されます。

注 例外が発生しない場合、このメソッドの出力は常に trueになります。

例外

lockTaken への入力は true です。

obj パラメーターが null です。

次のコードは、 メソッド オーバーロードを使用するための基本的なパターンを Enter(Object, Boolean) 示しています。 このオーバーロードは、メソッドが例外をスローした場合でも、 パラメーター (ByRef Visual Basic では ) lockTakenref渡される変数の値を常に設定するため、変数の値は、ロックを解放する必要があるかどうかをテストするための信頼性の高い方法です。

C#
bool acquiredLock = false;

try
{
    Monitor.Enter(lockObject, ref acquiredLock);

    // Code that accesses resources that are protected by the lock.
}
finally
{
    if (acquiredLock)
    {
        Monitor.Exit(lockObject);
    }
}

注釈

パラメーターとして渡された オブジェクトの をMonitor取得するには、 をobj使用Enterします。 別のスレッドが オブジェクトで を Enter 実行したが、対応する Exitをまだ実行していない場合、現在のスレッドは、他のスレッドが オブジェクトを解放するまでブロックします。 同じスレッドがブロックせずに複数回呼び出 Enter されることは有効ですが、オブジェクトで待機している他のスレッドがブロックを解除する前に、同じ数の Exit 呼び出しを呼び出す必要があります。

例外がスローされたためにロックが解除された場合、パラメーターに指定された変数は、このメソッドの lockTaken 終了後です false 。 これにより、プログラムはすべての場合において、ロックを解除する必要があるかどうかを判断できます。 このメソッドが例外をスローせずに を返す場合、パラメーターに指定された lockTaken 変数は常に trueであり、テストする必要はありません。

を使用して Monitor 、値型ではなくオブジェクト (つまり参照型) をロックします。 値型変数を に Enter渡すと、オブジェクトとしてボックス化されます。 同じ変数を再度 に Enter 渡すと、別のオブジェクトとしてボックス化され、スレッドはブロックされません。 この場合、保護していると Monitor 思われるコードは保護されません。 さらに、 変数を に Exit渡すと、別のオブジェクトが作成されます。 に渡されるオブジェクトは にExit渡されるオブジェクトとは異なるため、 は を MonitorEnterスローしますSynchronizationLockException。 詳細については、概念に関するトピック 「モニター」を参照してください。

Interrupt は、オブジェクトに を入力 Monitor するのを待機しているスレッドを中断できます。 ThreadInterruptedExceptionがスローされます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0