Mutex ã¯ã©ã¹
å®ç¾©
éè¦
ä¸é¨ã®æ å ±ã¯ããªãªã¼ã¹åã«å¤§ãã夿´ãããå¯è½æ§ããããã¬ãªãªã¼ã¹ããã製åã«é¢ãããã®ã§ãã Microsoft ã¯ãããã«è¨è¼ããã¦ããæ å ±ã«ã¤ãã¦ãæç¤ºã¾ãã¯é»ç¤ºãåãããä¸åä¿è¨¼ãã¾ããã
åæããªããã£ãã¯ãããã»ã¹éã®åæã«ã使ç¨ã§ãã¾ãã
public ref class Mutex sealed : System::Threading::WaitHandle
public sealed class Mutex : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Mutex : System.Threading.WaitHandle
type Mutex = class
inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type Mutex = class
inherit WaitHandle
Public NotInheritable Class Mutex
Inherits WaitHandle
- ç¶æ¿
- ç¶æ¿
- 屿§
ä¾
ãã®ä¾ã§ã¯ããã¼ã«ã« Mutex ãªãã¸ã§ã¯ãã使ç¨ãã¦ä¿è·ããããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãåæããæ¹æ³ã示ãã¾ãã åå¼ã³åºãå ã¹ã¬ããã¯ãã¥ã¼ããã¯ã¹ã®æææ¨©ãåå¾ããã¾ã§ãããã¯ãããããã ã¡ã½ããã ReleaseMutex å¼ã³åºãã¦ãã¥ã¼ããã¯ã¹ã®æææ¨©ãè§£æ¾ããå¿ è¦ãããã¾ãã
using System;
using System.Threading;
class Example
{
// 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 newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Name = String.Format("Thread{0}", i + 1);
newThread.Start();
}
// The main thread exits, but the application continues to
// run until all foreground threads have exited.
}
private static void ThreadProc()
{
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.
Console.WriteLine("{0} is requesting the mutex",
Thread.CurrentThread.Name);
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",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name);
}
}
// The example displays output like the following:
// Thread1 is requesting the mutex
// Thread2 is requesting the mutex
// Thread1 has entered the protected area
// Thread3 is requesting the mutex
// Thread1 is leaving the protected area
// Thread1 has released the mutex
// Thread3 has entered the protected area
// Thread3 is leaving the protected area
// Thread3 has released the mutex
// Thread2 has entered the protected area
// Thread2 is leaving the protected area
// Thread2 has released the mutex
Imports System.Threading
Module Example
' Create a new Mutex. The creating thread does not own the mutex.
Private mut As New Mutex()
Private Const numIterations As Integer = 1
Private Const numThreads As Integer = 3
Public Sub Main()
' Create the threads that will use the protected resource.
For i As Integer = 0 To numThreads - 1
Dim newThread As New Thread(AddressOf ThreadProc)
newThread.Name = String.Format("Thread{0}", i + 1)
newThread.Start()
Next
' The main thread exits, but the application continues to
' run until all foreground threads have exited.
End Sub
Private Sub ThreadProc()
For i As Integer = 0 To numIterations - 1
UseResource()
Next
End Sub
' This method represents a resource that must be synchronized
' so that only one thread at a time can enter.
Private Sub UseResource()
' Wait until it is safe to enter.
Console.WriteLine("{0} is requesting the mutex",
Thread.CurrentThread.Name)
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",
Thread.CurrentThread.Name)
' Release the Mutex.
mut.ReleaseMutex()
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name)
End Sub
End Module
' The example displays output like the following:
' Thread1 is requesting the mutex
' Thread2 is requesting the mutex
' Thread1 has entered the protected area
' Thread3 is requesting the mutex
' Thread1 is leaving the protected area
' Thread1 has released the mutex
' Thread3 has entered the protected area
' Thread3 is leaving the protected area
' Thread3 has released the mutex
' Thread2 has entered the protected area
' Thread2 is leaving the protected area
' Thread2 has released the mutex
次ã®ä¾ã§ã¯ãåã¹ã¬ããã ã¡ã½ããã WaitOne(Int32) å¼ã³åºãã¦ãã¥ã¼ããã¯ã¹ãåå¾ãã¾ãã ã¿ã¤ã ã¢ã¦ãééãçµéããã¨ã ã¡ã½ãã㯠ãè¿ false
ããã¹ã¬ããã¯ãã¥ã¼ããã¯ã¹ãåå¾ããããã¥ã¼ããã¯ã¹ãä¿è·ãããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãåå¾ãã¾ããã ã¡ã½ãã㯠ReleaseMutex ããã¥ã¼ããã¯ã¹ãåå¾ããã¹ã¬ããã«ãã£ã¦ã®ã¿å¼ã³åºããã¾ãã
using System;
using System.Threading;
class Example
{
// 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()
{
Example ex = new Example();
ex.StartThreads();
}
private void StartThreads()
{
// Create the threads that will use the protected resource.
for(int i = 0; i < numThreads; i++)
{
Thread newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Name = String.Format("Thread{0}", i + 1);
newThread.Start();
}
// The main thread returns to Main and exits, but the application continues to
// run until all foreground threads have exited.
}
private static void ThreadProc()
{
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, and do not enter if the request times out.
Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);
if (mut.WaitOne(1000)) {
Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// Simulate some work.
Thread.Sleep(5000);
Console.WriteLine("{0} is leaving the protected area",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name);
}
else {
Console.WriteLine("{0} will not acquire the mutex",
Thread.CurrentThread.Name);
}
}
~Example()
{
mut.Dispose();
}
}
// The example displays output like the following:
// Thread1 is requesting the mutex
// Thread1 has entered the protected area
// Thread2 is requesting the mutex
// Thread3 is requesting the mutex
// Thread2 will not acquire the mutex
// Thread3 will not acquire the mutex
// Thread1 is leaving the protected area
// Thread1 has released the mutex
Imports System.Threading
Class Example
' Create a new Mutex. The creating thread does not own the mutex.
Private mut As New Mutex()
Private Const numIterations As Integer = 1
Private Const numThreads As Integer = 3
Public Shared Sub Main()
Dim ex As New Example()
ex.StartThreads()
End Sub
Private Sub StartThreads()
' Create the threads that will use the protected resource.
For i As Integer = 0 To numThreads - 1
Dim newThread As New Thread(AddressOf ThreadProc)
newThread.Name = String.Format("Thread{0}", i + 1)
newThread.Start()
Next
' The main thread returns to Main and exits, but the application continues to
' run until all foreground threads have exited.
End Sub
Private Sub ThreadProc()
For i As Integer = 0 To numIterations - 1
UseResource()
Next
End Sub
' This method represents a resource that must be synchronized
' so that only one thread at a time can enter.
Private Sub UseResource()
' Wait until it is safe to enter.
Console.WriteLine("{0} is requesting the mutex",
Thread.CurrentThread.Name)
If mut.WaitOne(1000) Then
Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name)
' Place code to access non-reentrant resources here.
' Simulate some work.
Thread.Sleep(5000)
Console.WriteLine("{0} is leaving the protected area",
Thread.CurrentThread.Name)
' Release the Mutex.
mut.ReleaseMutex()
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name)
Else
Console.WriteLine("{0} will not acquire the mutex",
Thread.CurrentThread.Name)
End If
End Sub
Protected Overrides Sub Finalize()
mut.Dispose()
End Sub
End Class
' The example displays output like the following:
' Thread1 is requesting the mutex
' Thread1 has entered the protected area
' Thread2 is requesting the mutex
' Thread3 is requesting the mutex
' Thread2 will not acquire the mutex
' Thread3 will not acquire the mutex
' Thread1 is leaving the protected area
' Thread1 has released the mutex
注é
2 ã¤ä»¥ä¸ã®ã¹ã¬ãããåæã«å ±æãªã½ã¼ã¹ã«ã¢ã¯ã»ã¹ããå¿ è¦ãããå ´åãã·ã¹ãã ã«ã¯åæã¡ã«ããºã ãå¿ è¦ã§ãããä¸åº¦ã« 1 ã¤ã®ã¹ã¬ããã®ã¿ããªã½ã¼ã¹ã使ç¨ããããã«ãã¾ãã Mutex ã¯ãå ±æãªã½ã¼ã¹ã¸ã®æä»ã¢ã¯ã»ã¹ã 1 ã¤ã®ã¹ã¬ããã®ã¿ã«ä»ä¸ããåæããªããã£ãã§ãã ã¹ã¬ããããã¥ã¼ããã¯ã¹ãåå¾ããå ´åããã®ãã¥ã¼ããã¯ã¹ãåå¾ãã 2 çªç®ã®ã¹ã¬ããã¯ãæåã®ã¹ã¬ããããã¥ã¼ããã¯ã¹ãè§£æ¾ããã¾ã§ä¸æããã¾ãã
éè¦
ãã®å㯠IDisposable ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£
ãã¾ãã åã®ä½¿ç¨ãå®äºããããç´æ¥çã¾ãã¯éæ¥çã«åãç ´æ£ããå¿
è¦ãããã¾ãã ç´æ¥çã«åãç ´æ£ããã«ã¯ãtry
/catch
ãããã¯å
ã§ Dispose ã¡ã½ãããå¼ã³åºãã¾ãã 鿥çã«åãç ´æ£ããã«ã¯ãusing
(C# ã®å ´å) ã¾ã㯠Using
(Visual Basic è¨èª) ãªã©ã®è¨èªæ§æè¦ç´ ã使ç¨ãã¾ãã 詳細ã«ã¤ãã¦ã¯ãIDisposable ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«é¢ãããããã¯å
ã®ãIDisposable ãå®è£
ãããªãã¸ã§ã¯ãã®ä½¿ç¨ãã»ã¯ã·ã§ã³ãåç
§ãã¦ãã ããã
ã¡ã½ããã WaitHandle.WaitOne 使ç¨ãã¦ããã¥ã¼ããã¯ã¹ã®æææ¨©ãè¦æ±ã§ãã¾ãã å¼ã³åºãå ã®ã¹ã¬ããã¯ã次ã®ãããããçºçããã¾ã§ãããã¯ãã¾ãã
ãã¥ã¼ããã¯ã¹ã¯ãææããã¦ããªããã¨ã示ãã·ã°ãã«ãåãåãã¾ãã ãã®å ´åãã¡ã½ãã㯠ã WaitOne è¿
true
ããå¼ã³åºãå ã®ã¹ã¬ããã¯ãã¥ã¼ããã¯ã¹ã®æææ¨©ãå¼ãåãããã¥ã¼ããã¯ã¹ã«ãã£ã¦ä¿è·ããããªã½ã¼ã¹ã«ã¢ã¯ã»ã¹ãã¾ãã ãªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãå®äºããããã¹ã¬ãã㯠ã¡ã½ãããå¼ã³åºã㦠ReleaseMutex ãã¥ã¼ããã¯ã¹ã®æææ¨©ãè§£æ¾ããå¿ è¦ãããã¾ãã ãä¾ãã»ã¯ã·ã§ã³ã®æåã®ä¾ã¯ããã®ãã¿ã¼ã³ã示ãã¦ãã¾ããã¾ãã¯
timeout
ãã©ã¡ã¼ã¿ã¼ãæã¤millisecondsTimeout
ã¡ã½ããã®WaitOneå¼ã³åºãã§æå®ãããã¿ã¤ã ã¢ã¦ãééãçµéãã¾ããã ãã®å ´åãã¡ã½ãã㯠ã WaitOne è¿false
ããå¼ã³åºãå ã®ã¹ã¬ããã¯ãã¥ã¼ããã¯ã¹ã®æææ¨©ãåå¾ãããã¨ãã¾ããã ãã®å ´åã¯ããã¥ã¼ããã¯ã¹ã«ãã£ã¦ä¿è·ããã¦ãããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãå¼ã³åºãå ã¹ã¬ããã«å¯¾ãã¦æå¦ãããããã«ãã³ã¼ããæ§é åããå¿ è¦ãããã¾ãã ã¹ã¬ããã¯ãã¥ã¼ããã¯ã¹ã®æææ¨©ãåå¾ããªãã£ãããã ã¡ã½ããã ReleaseMutex å¼ã³åºãã¦ã¯ãªãã¾ããã ãä¾ãã»ã¯ã·ã§ã³ã® 2 çªç®ã®ä¾ã¯ããã®ãã¿ã¼ã³ã示ãã¦ãã¾ãã
ã¯ã©ã¹ã¯ Mutex ã¹ã¬ãã ID ãé©ç¨ããããããã¥ã¼ããã¯ã¹ã¯ããããåå¾ããã¹ã¬ããã«ãã£ã¦ã®ã¿è§£æ¾ã§ãã¾ãã ããã«å¯¾ãã ã¯ã©ã¹ã§ã¯ Semaphore ã¹ã¬ãã ID ã¯é©ç¨ããã¾ããã ãã¥ã¼ããã¯ã¹ã¯ãã¢ããªã±ã¼ã·ã§ã³ ãã¡ã¤ã³ã®å¢çãè¶ãã¦æ¸¡ããã¨ãã§ãã¾ãã
ãã¥ã¼ããã¯ã¹ãææããã¹ã¬ããã¯ãå®è¡ããããã¯ãããã¨ãªãã ã®ç¹°ãè¿ãå¼ã³åºãã§åããã¥ã¼ããã¯ã¹ã WaitOne è¦æ±ã§ãã¾ãã ãã ãããã¥ã¼ããã¯ã¹ã®æææ¨©ã ReleaseMutex è§£æ¾ããã«ã¯ãã¹ã¬ãããåãåæ°ã¡ã½ãããå¼ã³åºãå¿ è¦ãããã¾ãã
ã¯ã©ã¹ã¯ Mutex ãã WaitHandleç¶æ¿ããããããéç WaitHandle.WaitAll ã¡ã½ãã㨠WaitHandle.WaitAny ã¡ã½ãããå¼ã³åºãã¦ãä¿è·ããããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãåæãããã¨ãã§ãã¾ãã
ãã¥ã¼ããã¯ã¹ã®ææä¸ã«ã¹ã¬ãããçµäºããã¨ããã¥ã¼ããã¯ã¹ã¯ç ´æ£ãããã¨è¨ããã¾ãã ãã¥ã¼ããã¯ã¹ã®ç¶æ ã signaled ã«è¨å®ãããæ¬¡ã®å¾ æ©ã¹ã¬ãããæææ¨©ãåå¾ãã¾ãã .NET Frameworkã®ãã¼ã¸ã§ã³ 2.0 以éã§ã¯ãAbandonedMutexExceptionç ´æ£ããããã¥ã¼ããã¯ã¹ãåå¾ããæ¬¡ã®ã¹ã¬ããã§ ãã¹ãã¼ããã¾ãã .NET Frameworkã®ãã¼ã¸ã§ã³ 2.0 ããåã§ã¯ãä¾å¤ã¯ã¹ãã¼ããã¾ããã§ããã
注æäºé
ç ´æ£ããããã¥ã¼ããã¯ã¹ã¯ãå¤ãã®å ´åãã³ã¼ãã®é大ãªã¨ã©ã¼ã示ãã¾ãã ãã¥ã¼ããã¯ã¹ãè§£æ¾ããã«ã¹ã¬ãããçµäºããã¨ããã¥ã¼ããã¯ã¹ã«ãã£ã¦ä¿è·ããããã¼ã¿æ§é ãä¸è²«ããç¶æ ã§ã¯ãªãå¯è½æ§ãããã¾ãã ãã¥ã¼ããã¯ã¹ã®æææ¨©ãè¦æ±ããæ¬¡ã®ã¹ã¬ããã¯ããã®ä¾å¤ãå¦çãããã¼ã¿æ§é ã®æ´åæ§ãæ¤è¨¼ã§ããå ´åã¯ç¶è¡ã§ãã¾ãã
ã·ã¹ãã å ¨ä½ã§ãã¥ã¼ããã¯ã¹ãæå¹ãªå ´åã«ãã¥ã¼ããã¯ã¹ãç ´æ£ãããã¨ãã¯ãã¢ããªã±ã¼ã·ã§ã³ãå¼·å¶çµäºããããã¨ã示ãã¦ããå¯è½æ§ãããã¾ã (ãã¨ãã°ãWindows ã¿ã¹ã¯ ããã¼ã¸ã£ã使ç¨ããçµäº)ã
ãã¥ã¼ããã¯ã¹ã¯ãååã®ãªããã¼ã«ã« ãã¥ã¼ããã¯ã¹ã¨ååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã® 2 種é¡ã§ãã ãã¼ã«ã« ãã¥ã¼ããã¯ã¹ã¯ãç¾å¨ã®ããã»ã¹å ã«ã®ã¿åå¨ãã¾ãã ããã¯ããã¥ã¼ããã¯ã¹ã表ããªãã¸ã§ã¯ãã¸ã®åç §ãæã¤ããã»ã¹å ã®ä»»æã® Mutex ã¹ã¬ããã«ãã£ã¦ä½¿ç¨ã§ãã¾ãã ååã®ãªãåãªãã¸ã§ã¯ã㯠Mutex ãåå¥ã®ãã¼ã«ã« ãã¥ã¼ããã¯ã¹ã表ãã¾ãã
ååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã¯ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã å ¨ä½ã§è¡¨ç¤ºãããããã»ã¹ã®ã¢ã¯ãã£ããã£ãåæããããã«ä½¿ç¨ã§ãã¾ãã ååã Mutex åãå ¥ããã³ã³ã¹ãã©ã¯ã¿ã¼ã使ç¨ãã¦ãååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã表ã ãªãã¸ã§ã¯ãã使ã§ãã¾ãã ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ãªãã¸ã§ã¯ãã¯åæã«ä½æãããã¨ãããªãã¸ã§ã¯ãã使 Mutex ããåã«åå¨ãããã¨ãã§ãã¾ãã åãååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã表ãè¤æ°ã® Mutex ãªãã¸ã§ã¯ãã使ã§ãã¾ããã¾ããOpenExisting ã¡ã½ããã使ç¨ãã¦ãæ¢åã®ååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ãéããã¨ãã§ãã¾ãã
Note
ã¿ã¼ããã« ãµã¼ãã¹ãå®è¡ãã¦ãããµã¼ãã¼ã§ã¯ãååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã« 2 ã¤ã®ã¬ãã«ã®å¯è¦æ§ãè¨å®ã§ãã¾ãã ååã ãã¬ãã£ãã¯ã¹ Global\
ã§å§ã¾ãå ´åããã¥ã¼ããã¯ã¹ã¯ãã¹ã¦ã®ã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³ã§è¡¨ç¤ºããã¾ãã ååã ãã¬ãã£ãã¯ã¹ Local\
ã§å§ã¾ãå ´åããã¥ã¼ããã¯ã¹ã¯ä½æãããã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³ã§ã®ã¿è¡¨ç¤ºããã¾ãã ãã®å ´åãåãååã®åå¥ã®ãã¥ã¼ããã¯ã¹ãããµã¼ãã¼ä¸ã®ä»ã®åã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³ã«åå¨ããå¯è½æ§ãããã¾ãã ååä»ããã¥ã¼ããã¯ã¹ã使ããã¨ãã«ãã¬ãã£ãã¯ã¹ãæå®ããªãå ´åã¯ããã¬ãã£ãã¯ã¹ Local\
ãåãåãã¾ãã ã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³å
ã§ã¯ããã¬ãã£ãã¯ã¹ã«ãã£ã¦ã®ã¿ååãç°ãªã 2 ã¤ã®ãã¥ã¼ããã¯ã¹ã¯åå¥ã®ãã¥ã¼ããã¯ã¹ã§ããã両æ¹ã¨ãã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³å
ã®ãã¹ã¦ã®ããã»ã¹ã«è¡¨ç¤ºããã¾ãã ã¤ã¾ãããã¬ãã£ãã¯ã¹å Global\
㨠Local\
ãããã»ã¹ã«å¯¾ããç¸å¯¾ã§ã¯ãªããã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³ã«å¯¾ãããã¥ã¼ããã¯ã¹åã®ã¹ã³ã¼ããè¨è¿°ãã¾ãã
注æäºé
æ¢å®ã§ã¯ãååä»ããã¥ã¼ããã¯ã¹ã¯ãããã使ããã¦ã¼ã¶ã¼ã«å¶éããã¾ããã ä»ã®ã¦ã¼ã¶ã¼ã¯ããã¥ã¼ããã¯ã¹ãéãã¦ä½¿ç¨ã§ããå ´åãããã¾ãããã¥ã¼ããã¯ã¹ã«å ¥ã£ã¦ãã¥ã¼ããã¯ã¹ãçµäºããªããã¨ã«ãããã¥ã¼ããã¯ã¹ã®å¹²æ¸ãªã©ã§ãã Unix ã«ä¼¼ããªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ã§ã¯ããã¡ã¤ã« ã·ã¹ãã ã¯ååä»ããã¥ã¼ããã¯ã¹ã®å®è£ ã§ä½¿ç¨ãããä»ã®ã¦ã¼ã¶ã¼ã¯ãããéè¦ãªæ¹æ³ã§ååä»ããã¥ã¼ããã¯ã¹ã«å¹²æ¸ã§ããå¯è½æ§ãããã¾ãã Windows ã§ã¯ãç¹å®ã®ã¦ã¼ã¶ã¼ã¸ã®ã¢ã¯ã»ã¹ãå¶éããããã«ãã³ã³ã¹ãã©ã¯ã¿ã¼ã®ãªã¼ãã¼ãã¼ãã使ç¨ããã MutexAcl ãååä»ããã¥ã¼ããã¯ã¹ã使ããã¨ãã« ãæ¸¡ããã¨ãã§ãã¾ã MutexSecurity ã Unix ã«ä¼¼ããªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ã§ã¯ãç¾å¨ãååä»ããã¥ã¼ããã¯ã¹ã¸ã®ã¢ã¯ã»ã¹ãå¶éããæ¹æ³ã¯ããã¾ããã ä¿¡é ¼ããã¦ããªãã¦ã¼ã¶ã¼ãã³ã¼ããå®è¡ãã¦ããå¯è½æ§ãããã·ã¹ãã ã§ã¯ãã¢ã¯ã»ã¹å¶éãªãã§ååä»ããã¥ã¼ããã¯ã¹ã使ç¨ããªãã§ãã ããã
åè¨å· (\) ã¯ãã¥ã¼ããã¯ã¹åã®äºç´æåã§ãã ã¿ã¼ããã« ãµã¼ãã¼ ã»ãã·ã§ã³ã§ã®ãã¥ã¼ããã¯ã¹ã®ä½¿ç¨ã«é¢ããã¡ã¢ã§æå®ããã¦ããå ´åãé¤ãããã¥ã¼ããã¯ã¹åã«åè¨å· (\) ã使ç¨ããªãã§ãã ããã 使ç¨ããå ´åããã¥ã¼ããã¯ã¹ã®ååãæ¢åã®ãã¡ã¤ã«ã表ãã¨ãã¦ããDirectoryNotFoundException ãã¹ãã¼ããããã¨ãããã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã¼
Mutex() |
Mutex ã¯ã©ã¹ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ããæ¢å®ã®ããããã£ã使ç¨ãã¦åæåãã¾ãã |
Mutex(Boolean) |
å¼ã³åºãå ã®ã¹ã¬ããã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ããããã©ããã示ããã¼ã«å¤ã使ç¨ãã¦ãMutex ã¯ã©ã¹ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ãåæåãã¾ãã |
Mutex(Boolean, String) |
å¼ã³åºãå ã®ã¹ã¬ããã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ããããã©ããã示ããã¼ã«å¤ã¨ããã¥ã¼ããã¯ã¹ã®ååã表ãæååã使ç¨ãã¦ãMutex ã¯ã©ã¹ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ãåæåãã¾ãã |
Mutex(Boolean, String, Boolean) |
å¼ã³åºãå ã®ã¹ã¬ããã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ããããã©ããã示ããã¼ã«å¤ããã¥ã¼ããã¯ã¹ã®ååã表ãæååãããã³ã¡ã½ããããæ»ãã¨ãã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ãå¼ã³åºãå ã®ã¹ã¬ããã«ä»ä¸ããããã©ããã示ããã¼ã«å¤ãæå®ãã¦ãMutex ã¯ã©ã¹ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ãåæåãã¾ãã |
Mutex(Boolean, String, Boolean, MutexSecurity) |
å¼ã³åºãå ã®ã¹ã¬ããã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ããããã©ããã示ããã¼ã«å¤ããã¥ã¼ããã¯ã¹ã®ååã表ãæååãã¡ã½ãããæ»ãã¨ãã«ãã¥ã¼ããã¯ã¹ã®åææææ¨©ãå¼ã³åºãå ã®ã¹ã¬ããã«ä»ä¸ããããã©ããã示ããã¼ã«å¤å¤æ°ãããã³ååä»ããã¥ã¼ããã¯ã¹ã«é©ç¨ããã¢ã¯ã»ã¹å¶å¾¡ã»ãã¥ãªãã£ãæå®ãã¦ãMutex ã¯ã©ã¹ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ãåæåãã¾ãã |
ãã£ã¼ã«ã
WaitTimeout |
å¾ æ©ãã³ãã«ãã·ã°ãã«ç¶æ ã«ãªãåã« WaitAny(WaitHandle[], Int32, Boolean) æä½ãã¿ã¤ã ã¢ã¦ãã«ãªã£ããã¨ã示ãã¾ãã ãã®ãã£ã¼ã«ãã¯å®æ°ã§ãã (ç¶æ¿å WaitHandle) |
ããããã£
Handle |
å¤ã.
å¤ã.
ãã¤ãã£ã ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ãã³ãã«ãåå¾ã¾ãã¯è¨å®ãã¾ãã (ç¶æ¿å WaitHandle) |
SafeWaitHandle |
ãã¤ãã£ã ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ãã³ãã«ãåå¾ã¾ãã¯è¨å®ãã¾ãã (ç¶æ¿å WaitHandle) |
ã¡ã½ãã
Close() |
ç¾å¨ã® WaitHandle ã«ãã£ã¦ä¿æããã¦ãããã¹ã¦ã®ãªã½ã¼ã¹ãè§£æ¾ãã¾ãã (ç¶æ¿å WaitHandle) |
CreateObjRef(Type) |
ãªã¢ã¼ã ãªãã¸ã§ã¯ãã¨ã®éä¿¡ã«ä½¿ç¨ãããããã·ã®çæã«å¿ è¦ãªæ å ±ããã¹ã¦æ ¼ç´ãã¦ãããªãã¸ã§ã¯ãã使ãã¾ãã (ç¶æ¿å MarshalByRefObject) |
Dispose() |
WaitHandle ã¯ã©ã¹ã®ç¾å¨ã®ã¤ã³ã¹ã¿ã³ã¹ã«ãã£ã¦ä½¿ç¨ããã¦ãããã¹ã¦ã®ãªã½ã¼ã¹ãè§£æ¾ãã¾ãã (ç¶æ¿å WaitHandle) |
Dispose(Boolean) |
æ´¾çã¯ã©ã¹ã§ãªã¼ãã¼ã©ã¤ããããã¨ãWaitHandle ã«ãã£ã¦ä½¿ç¨ããã¦ããã¢ã³ããã¼ã¸ã ãªã½ã¼ã¹ãè§£æ¾ãããªãã·ã§ã³ã§ããã¼ã¸ã ãªã½ã¼ã¹ãè§£æ¾ãã¾ãã (ç¶æ¿å WaitHandle) |
Equals(Object) |
æå®ããããªãã¸ã§ã¯ããç¾å¨ã®ãªãã¸ã§ã¯ãã¨çãããã©ããã夿ãã¾ãã (ç¶æ¿å Object) |
GetAccessControl() |
ååä»ããã¥ã¼ããã¯ã¹ã®ã¢ã¯ã»ã¹å¶å¾¡ã»ãã¥ãªãã£ã表ã MutexSecurity ãªãã¸ã§ã¯ããåå¾ãã¾ãã |
GetHashCode() |
æ¢å®ã®ããã·ã¥é¢æ°ã¨ãã¦æ©è½ãã¾ãã (ç¶æ¿å Object) |
GetLifetimeService() |
å¤ã.
対象ã®ã¤ã³ã¹ã¿ã³ã¹ã®æå¹æéããªã·ã¼ãå¶å¾¡ãããç¾å¨ã®æå¹æéãµã¼ãã¹ ãªãã¸ã§ã¯ããåå¾ãã¾ãã (ç¶æ¿å MarshalByRefObject) |
GetType() |
ç¾å¨ã®ã¤ã³ã¹ã¿ã³ã¹ã® Type ãåå¾ãã¾ãã (ç¶æ¿å Object) |
InitializeLifetimeService() |
å¤ã.
ãã®ã¤ã³ã¹ã¿ã³ã¹ã®æå¹æéããªã·ã¼ãå¶å¾¡ããæå¹æéãµã¼ãã¹ ãªãã¸ã§ã¯ããåå¾ãã¾ãã (ç¶æ¿å MarshalByRefObject) |
MemberwiseClone() |
ç¾å¨ã® Object ã®ç°¡æã³ãã¼ã使ãã¾ãã (ç¶æ¿å Object) |
MemberwiseClone(Boolean) |
ç¾å¨ã® MarshalByRefObject ãªãã¸ã§ã¯ãã®ç°¡æã³ãã¼ã使ãã¾ãã (ç¶æ¿å MarshalByRefObject) |
OpenExisting(String) |
æ¢ã«åå¨ããå ´åã¯ãæå®ããååä»ããã¥ã¼ããã¯ã¹ãéãã¾ãã |
OpenExisting(String, MutexRights) |
æ¢ã«åå¨ããå ´åã¯ãå¿ è¦ãªã»ãã¥ãªã㣠ã¢ã¯ã»ã¹ã§æå®ããååä»ããã¥ã¼ããã¯ã¹ãéãã¾ãã |
ReleaseMutex() |
Mutex ãä¸åº¦è§£æ¾ãã¾ãã |
SetAccessControl(MutexSecurity) |
ååä»ãã·ã¹ãã ãã¥ã¼ããã¯ã¹ã®ã¢ã¯ã»ã¹å¶å¾¡ã»ãã¥ãªãã£ãè¨å®ãã¾ãã |
ToString() |
ç¾å¨ã®ãªãã¸ã§ã¯ãã表ãæååãè¿ãã¾ãã (ç¶æ¿å Object) |
TryOpenExisting(String, Mutex) |
æ¢ã«åå¨ããå ´åã¯ãæå®ããååä»ããã¥ã¼ããã¯ã¹ãéãæä½ãæåãããã©ããã示ãå¤ãè¿ãã¾ãã |
TryOpenExisting(String, MutexRights, Mutex) |
æ¢ã«åå¨ããå ´åã¯ãå¿ è¦ãªã»ãã¥ãªã㣠ã¢ã¯ã»ã¹ã使ç¨ãã¦æå®ããååä»ããã¥ã¼ããã¯ã¹ãéããæä½ãæåãããã©ããã示ãå¤ãè¿ãã¾ãã |
WaitOne() |
ç¾å¨ã® WaitHandle ãã·ã°ãã«ãåãåãã¾ã§ãç¾å¨ã®ã¹ã¬ããããããã¯ãã¾ãã (ç¶æ¿å WaitHandle) |
WaitOne(Int32) |
32 ããã符å·ä»ãæ´æ°ã使ç¨ãã¦æéééãããªç§åä½ã§æå®ããç¾å¨ã® WaitHandle ãã·ã°ãã«ãåä¿¡ããã¾ã§ãç¾å¨ã®ã¹ã¬ããããããã¯ãã¾ãã (ç¶æ¿å WaitHandle) |
WaitOne(Int32, Boolean) |
ç¾å¨ã® WaitHandle ãã·ã°ãã«ãåä¿¡ããã¾ã§ç¾å¨ã®ã¹ã¬ããããããã¯ãã¾ããæéééãæå®ããããã« 32 ããã符å·ä»ãæ´æ°ã使ç¨ããå¾ æ©ã®åã§ãåæãã¡ã¤ã³ãçµäºãããã©ãããæå®ãã¾ãã (ç¶æ¿å WaitHandle) |
WaitOne(TimeSpan) |
TimeSpan ã使ç¨ãã¦æéééãæå®ããç¾å¨ã®ã¤ã³ã¹ã¿ã³ã¹ãã·ã°ãã«ãåä¿¡ããã¾ã§ç¾å¨ã®ã¹ã¬ããããããã¯ãã¾ãã (ç¶æ¿å WaitHandle) |
WaitOne(TimeSpan, Boolean) |
ç¾å¨ã®ã¤ã³ã¹ã¿ã³ã¹ãã·ã°ãã«ãåä¿¡ããã¾ã§ç¾å¨ã®ã¹ã¬ããããããã¯ãã¾ããTimeSpan ã使ç¨ãã¦æéééãæå®ããå¾ æ©ã®åã§ãåæãã¡ã¤ã³ãçµäºãããã©ãããæå®ãã¾ãã (ç¶æ¿å WaitHandle) |
æç¤ºçãªã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å®è£
IDisposable.Dispose() |
ãã® API ã¯è£½åã¤ã³ãã©ã¹ãã©ã¯ãã£ããµãã¼ããã¾ããã³ã¼ãããç´æ¥ä½¿ç¨ãããã®ã§ã¯ããã¾ããã WaitHandle ã«ãã£ã¦ä½¿ç¨ããã¦ãããã¹ã¦ã®ãªã½ã¼ã¹ãè§£æ¾ãã¾ãã (ç¶æ¿å WaitHandle) |
æ¡å¼µã¡ã½ãã
GetAccessControl(Mutex) |
æå®ãã |
SetAccessControl(Mutex, MutexSecurity) |
æå®ãããã¥ã¼ããã¯ã¹ã®ã»ãã¥ãªãã£è¨è¿°åãè¨å®ãã¾ãã |
GetSafeWaitHandle(WaitHandle) |
ãã¤ãã£ã ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ã®å¾ æ©ãã³ãã«ã®ããã®ã»ã¼ã ãã³ãã«ãåå¾ãã¾ãã |
SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
ãã¤ãã£ã ãªãã¬ã¼ãã£ã³ã° ã·ã¹ãã ã®å¾ æ©ãã³ãã«ã®ããã®ã»ã¼ã ãã³ãã«ãè¨å®ãã¾ãã |
é©ç¨å¯¾è±¡
ã¹ã¬ãã ã»ã¼ã
ãã®åã¯ã¹ã¬ãã ã»ã¼ãã§ãã