Mutex クラス
アセンブリ: mscorlib (mscorlib.dll 内)


![]() |
---|
放棄されたミューテックスは、コードに深刻なエラーが存在することを意味します。スレッドがミューテックスを解放せずに終了すると、ミューテックスによって保護されるデータ構造の状態に不整合が生じることがあります。ミューテックスの所有権を要求する次のスレッドが、データ構造の整合性を検証できる場合、この例外を処理して続行できます。 |
ミューテックスには、ローカル ミューテックスと名前付きシステム ミューテックスの 2 種類があります。名前を受け入れるコンストラクタを使用して Mutex オブジェクトを作成した場合、オブジェクトはその名前のオペレーティング システム オブジェクトに関連付けられます。名前付きシステム ミューテックスはオペレーティング システム全体から参照でき、プロセスの動作を同期するために使用できます。同じ名前付きシステム ミューテックスを表す複数の Mutex オブジェクトを作成できます。また、OpenExisting メソッドを使用すると、既存の名前付きシステム ミューテックスを開くことができます。
ローカル ミューテックスはプロセス内のみに存在します。これは、ローカル Mutex オブジェクトへの参照を持つプロセス内のすべてのスレッドで使用できます。各 Mutex オブジェクトは個別のローカル ミューテックスです。

' This example shows how a Mutex is used to synchronize access ' to a protected resource. Unlike Monitor, Mutex can be used with ' WaitHandle.WaitAll and WaitAny, and can be passed across ' AppDomain boundaries. Imports System Imports System.Threading Imports Microsoft.VisualBasic Class Test ' Create a new Mutex. The creating thread does not own the ' Mutex. Private Shared mut As New Mutex() Private Const numIterations As Integer = 1 Private Const numThreads As Integer = 3 <MTAThread> _ Shared Sub Main() ' Create the threads that will use the protected resource. Dim i As Integer For i = 1 To numThreads Dim myThread As New Thread(AddressOf MyThreadProc) myThread.Name = [String].Format("Thread{0}", i) myThread.Start() Next i ' The main thread exits, but the application continues to ' run until all foreground threads have exited. End Sub 'Main Private Shared Sub MyThreadProc() Dim i As Integer For i = 1 To numIterations UseResource() Next i End Sub 'MyThreadProc ' This method represents a resource that must be synchronized ' so that only one thread at a time can enter. Private Shared Sub UseResource() ' Wait until it is safe to enter. mut.WaitOne() Console.WriteLine("{0} has entered protected area", _ Thread.CurrentThread.Name) ' Place code to access non-reentrant resources here. ' Simulate some work Thread.Sleep(500) Console.WriteLine("{0} is leaving protected area" & vbCrLf, _ Thread.CurrentThread.Name) ' Release Mutex. mut.ReleaseMutex() End Sub 'UseResource End Class 'MyMainClass
// This example shows how a Mutex is used to synchronize access // to a protected resource. Unlike Monitor, Mutex can be used with // WaitHandle.WaitAll and WaitAny, and can be passed across // AppDomain boundaries. using System; using System.Threading; class Test { // Create a new Mutex. The creating thread does not own the // Mutex. private static Mutex mut = new Mutex(); private const int numIterations = 1; private const int numThreads = 3; static void Main() { // Create the threads that will use the protected resource. for(int i = 0; i < numThreads; i++) { Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } // The main thread exits, but the application continues to // run until all foreground threads have exited. } private static void MyThreadProc() { for(int i = 0; i < numIterations; i++) { UseResource(); } } // This method represents a resource that must be synchronized // so that only one thread at a time can enter. private static void UseResource() { // Wait until it is safe to enter. mut.WaitOne(); Console.WriteLine("{0} has entered the protected area", Thread.CurrentThread.Name); // Place code to access non-reentrant resources here. // Simulate some work. Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area\r\n", Thread.CurrentThread.Name); // Release the Mutex. mut.ReleaseMutex(); } }
// This example shows how a Mutex is used to synchronize access // to a protected resource. Unlike Monitor, Mutex can be used with // WaitHandle.WaitAll and WaitAny, and can be passed across // AppDomain boundaries. using namespace System; using namespace System::Threading; const int numIterations = 1; const int numThreads = 3; ref class Test { public: // Create a new Mutex. The creating thread does not own the // Mutex. static Mutex^ mut = gcnew Mutex; static void MyThreadProc() { for ( int i = 0; i < numIterations; i++ ) { UseResource(); } } private: // This method represents a resource that must be synchronized // so that only one thread at a time can enter. static void UseResource() { //Wait until it is OK to enter. mut->WaitOne(); Console::WriteLine( "{0} has entered protected the area", Thread::CurrentThread->Name ); // Place code to access non-reentrant resources here. // Simulate some work. Thread::Sleep( 500 ); Console::WriteLine( "{0} is leaving protected the area\r\n", Thread::CurrentThread->Name ); // Release the Mutex. mut->ReleaseMutex(); } }; int main() { // Create the threads that will use the protected resource. for ( int i = 0; i < numThreads; i++ ) { Thread^ myThread = gcnew Thread( gcnew ThreadStart( Test::MyThreadProc ) ); myThread->Name = String::Format( "Thread {0}", i + 1 ); myThread->Start(); } // The main thread exits, but the application continues to // run until all foreground threads have exited. }
// This example shows how a Mutex is used to synchronize access // to a protected resource. Unlike Monitor, Mutex can be used with // WaitHandle.WaitAll and WaitAny, and can be passed across // AppDomain boundaries. import System.*; import System.Threading.*; import System.Threading.Thread; class Test { // Create a new Mutex. The creating thread does not own the // Mutex. private static Mutex mut = new Mutex(); private static int numIterations = 1; private static int numThreads = 3; public static void main(String[] args) { // Create the threads that will use the protected resource. for (int i = 0; i < numThreads; i++) { Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.set_Name(String.Format("Thread{0}", String.valueOf(i + 1))); myThread.Start(); } } //main // The main thread exits, but the application continues to // run until all foreground threads have exited. private static void MyThreadProc() { for (int i = 0; i < numIterations; i++) { UseResource(); } } //MyThreadProc // This method represents a resource that must be synchronized // so that only one thread at a time can enter. private static void UseResource() { // Wait until it is safe to enter. mut.WaitOne(); Console.WriteLine("{0} has entered the protected area", Thread.get_CurrentThread().get_Name()); // Place code to access non-reentrant resources here. // Simulate some work. Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area\r\n", Thread.get_CurrentThread().get_Name()); // Release the Mutex. mut.ReleaseMutex(); } //UseResource } //Test

System.MarshalByRefObject
System.Threading.WaitHandle
System.Threading.Mutex


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Mutex クラスのページへのリンク