Mutex.ReleaseMutex 메서드
Mutex를 한 번 해제합니다.
네임스페이스: System.Threading
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
Public Sub ReleaseMutex
‘사용 방법
Dim instance As Mutex
instance.ReleaseMutex
public void ReleaseMutex ()
public:
void ReleaseMutex ()
public void ReleaseMutex ()
public function ReleaseMutex ()
예외
예외 형식 | 조건 |
---|---|
호출한 스레드가 뮤텍스를 소유하지 않은 경우 |
설명
뮤텍스를 소유한 스레드는 실행을 차단하지 않고 반복된 대기 함수 호출에서 같은 뮤텍스를 지정할 수 있습니다. 호출 횟수는 공용 언어 런타임에서 저장합니다. 뮤텍스의 소유권을 해제하려면 스레드에서 ReleaseMutex를 같은 횟수만큼 호출해야 합니다.
뮤텍스를 소유하고 있는 동안 스레드가 종료되면 뮤텍스가 중단됩니다. 뮤텍스 상태는 신호 받음으로 설정되고 대기 중인 다음 스레드가 소유권을 가져옵니다. 뮤텍스를 소유하는 스레드가 없는 경우 뮤텍스의 상태는 신호 받음으로 설정됩니다. .NET Framework 버전 2.0부터는 뮤텍스를 가져오는 다음 스레드에서 AbandonedMutexException이 throw됩니다. .NET Framework 2.0 이전 버전에서는 예외가 throw되지 않았습니다.
경고
뮤텍스가 중단되면 코드에 심각한 오류가 있는 것입니다. 뮤텍스를 해제하지 않고 스레드를 종료하면 뮤텍스가 보호하는 데이터 구조의 상태에 일관성이 없을 수 있습니다. 데이터 구조의 무결성을 확인할 수 있으면 뮤텍스의 소유권을 요청하는 다음 스레드가 이 예외를 처리한 다음 계속할 수 있습니다.
예제
' 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
플랫폼
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원
참고 항목
참조
Mutex 클래스
Mutex 멤버
System.Threading 네임스페이스