呼び出し元のスレッドにミューテックスの初期所有権があるかどうかを示す Boolean 型の値を使用して、Mutex クラスの新しいインスタンスを初期化します。
名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
initiallyOwned As Boolean _
)
'使用
Dim initiallyOwned As Boolean
Dim instance As New Mutex(initiallyOwned)
public Mutex (
bool initiallyOwned
)
public:
Mutex (
bool initiallyOwned
)
public Mutex (
boolean initiallyOwned
)
public function Mutex (
initiallyOwned : boolean
)
適用できません。
パラメータ
- initiallyOwned
呼び出し元スレッドにミューテックスの初期所有権を与える場合はtrue。それ以外の場合は false。
使用例
ローカル 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 owns the
' Mutex.
Private Shared mut As New Mutex(True)
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
' Wait one second before allowing other threads to
' acquire the Mutex.
Console.WriteLine("Creating thread owns the Mutex.")
Thread.Sleep(1000)
Console.WriteLine("Creating thread releases the Mutex." & vbCrLf)
mut.ReleaseMutex()
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 owns the
// Mutex.
private static Mutex mut = new Mutex(true);
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();
}
// Wait one second before allowing other threads to
// acquire the Mutex.
Console.WriteLine("Creating thread owns the Mutex.");
Thread.Sleep(1000);
Console.WriteLine("Creating thread releases the Mutex.\r\n");
mut.ReleaseMutex();
}
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 owns the
// Mutex.
static Mutex^ mut = gcnew Mutex( true );
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()
{
// Initialize the Mutex.
Mutex^ mut = Test::mut;
// 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();
}
// Wait one second before allowing other threads to
// acquire the Mutex.
Console::WriteLine( "Creating thread owns the Mutex." );
Thread::Sleep( 1000 );
Console::WriteLine( "Creating thread releases the Mutex.\r\n" );
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.
import System.*;
import System.Threading.* ;
import System.Threading.Thread;
class Test
{
// Create a new Mutex. The creating thread owns the
// Mutex.
private static Mutex mut = new Mutex(true);
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();
}
// Wait one second before allowing other threads to
// acquire the Mutex.
Console.WriteLine("Creating thread owns the Mutex.");
Thread.Sleep(1000);
Console.WriteLine("Creating thread releases the Mutex.\r\n");
mut.ReleaseMutex();
} //main
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
プラットフォーム
Windows 98,Windows Server 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
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
.NET Compact Framework
サポート対象 : 2.0,1.0
XNA Framework
サポート対象 : 1.0
参照
関連項目
Mutex クラス
Mutex メンバ
System.Threading 名前空間