AbandonedMutexException 클래스

정의

스레드가 다른 스레드에서 해제하지 않고 종료하여 중단한 Mutex 개체를 가져오면 throw되는 예외입니다.

public ref class AbandonedMutexException : Exception
public ref class AbandonedMutexException : SystemException
public class AbandonedMutexException : Exception
public class AbandonedMutexException : SystemException
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class AbandonedMutexException : SystemException
type AbandonedMutexException = class
    inherit Exception
type AbandonedMutexException = class
    inherit SystemException
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type AbandonedMutexException = class
    inherit SystemException
Public Class AbandonedMutexException
Inherits Exception
Public Class AbandonedMutexException
Inherits SystemException
상속
AbandonedMutexException
상속
AbandonedMutexException
특성

예제

다음 코드 예제에서는 5개의 뮤텍스를 중단하는 스레드를 실행하여 , WaitAnyWaitAll 메서드에 미치는 WaitOne영향을 보여 줍니다. 호출에 대한 MutexIndex 속성 값이 WaitAny 표시됩니다.

참고

메서드 호출 WaitAny 이 중단된 뮤텍스 중 하나에 의해 중단됩니다. 다른 버려진 뮤텍스는 후속 대기 메서드에 AbandonedMutexException 의해 throw될 수 있습니다.

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Example
    {
    private:
        static ManualResetEvent^ dummyEvent = 
            gcnew ManualResetEvent(false);
            
        static Mutex^ orphanMutex1 = gcnew Mutex;
        static Mutex^ orphanMutex2 = gcnew Mutex;
        static Mutex^ orphanMutex3 = gcnew Mutex;
        static Mutex^ orphanMutex4 = gcnew Mutex;
        static Mutex^ orphanMutex5 = gcnew Mutex;
        
    public:
        static void ProduceAbandonMutexException(void)
        {
            
            // Start a thread that grabs all five mutexes, and then
            // abandons them.
            Thread^ abandonThread = 
                gcnew Thread(gcnew ThreadStart(AbandonMutex));

            abandonThread->Start();
            
            // Make sure the thread is finished.
            abandonThread->Join();
            
            // Wait on one of the abandoned mutexes. The WaitOne
            // throws an AbandonedMutexException.
            try
            {
                orphanMutex1->WaitOne();
                Console::WriteLine("WaitOne succeeded.");
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitOne: {0}", 
                    ex->Message);
            }
            finally
            {
                
                // Whether or not the exception was thrown, 
                // the current thread owns the mutex, and 
                // must release it.
                orphanMutex1->ReleaseMutex();
            }

            
            // Create an array of wait handles, consisting of one
            // ManualResetEvent and two mutexes, using two more of
            // the abandoned mutexes.
            array <WaitHandle^>^ waitFor = {dummyEvent, 
                orphanMutex2, orphanMutex3};
            
            // WaitAny returns when any of the wait handles in the 
            // array is signaled. Either of the two abandoned mutexes
            // satisfy the wait, but lower of the two index values is
            // returned by MutexIndex. Note that the Try block and
            // the Catch block obtain the index in different ways.
            try
            {
                int index = WaitHandle::WaitAny(waitFor);
                Console::WriteLine("WaitAny succeeded.");
                (safe_cast<Mutex^>(waitFor[index]))->ReleaseMutex();
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitAny at index {0}"
                    "\r\n\tMessage: {1}", ex->MutexIndex, 
                    ex->Message);
                (safe_cast<Mutex^>(waitFor[ex->MutexIndex]))->
                    ReleaseMutex();
            }

            orphanMutex3->ReleaseMutex();
            
            // Use two more of the abandoned mutexes for the WaitAll 
            // call. WaitAll doesn't return until all wait handles 
            // are signaled, so the ManualResetEvent must be signaled 
            // by calling Set().
            dummyEvent->Set();
            waitFor[1] = orphanMutex4;
            waitFor[2] = orphanMutex5;
            
            // Because WaitAll requires all the wait handles to be
            // signaled, both mutexes must be released even if the
            // exception is thrown. Thus, the ReleaseMutex calls are 
            // placed in the Finally block. Again, MutexIndex returns
            // the lower of the two index values for the abandoned
            // mutexes.
            //  
            try
            {
                WaitHandle::WaitAll(waitFor);
                Console::WriteLine("WaitAll succeeded.");
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitAny at index {0}"
                    "\r\n\tMessage: {1}", ex->MutexIndex, 
                    ex->Message);
            }
            finally
            {
                orphanMutex4->ReleaseMutex();
                orphanMutex5->ReleaseMutex();
            }

        }


    private:
        [MTAThread]
        static void AbandonMutex()
        {
            orphanMutex1->WaitOne();
            orphanMutex2->WaitOne();
            orphanMutex3->WaitOne();
            orphanMutex4->WaitOne();
            orphanMutex5->WaitOne();
            Console::WriteLine(
                "Thread exits without releasing the mutexes.");
        }
    };   
}

//Entry point of example application
[MTAThread]
int main(void)
{
    SystemThreadingExample::Example::ProduceAbandonMutexException();
}

// This code example produces the following output:
// Thread exits without releasing the mutexes.
// Exception in WaitOne: The wait completed due to an abandoned mutex.
// Exception in WaitAny at index 1
//         Message: The wait completed due to an abandoned mutex.
// Exception in WaitAll at index -1
//         Message: The wait completed due to an abandoned mutex.

using System;
using System.Threading;

public class Example
{
    private static ManualResetEvent _dummy = new ManualResetEvent(false);

    private static Mutex _orphan1 = new Mutex();
    private static Mutex _orphan2 = new Mutex();
    private static Mutex _orphan3 = new Mutex();
    private static Mutex _orphan4 = new Mutex();
    private static Mutex _orphan5 = new Mutex();

    [MTAThread]
    public static void Main()
    {
        // Start a thread that takes all five mutexes, and then
        // ends without releasing them.
        //
        Thread t = new Thread(new ThreadStart(AbandonMutex));
        t.Start();
        // Make sure the thread is finished.
        t.Join();

        // Wait on one of the abandoned mutexes. The WaitOne returns
        // immediately, because its wait condition is satisfied by
        // the abandoned mutex, but on return it throws
        // AbandonedMutexException.
        try
        {
            _orphan1.WaitOne();
            Console.WriteLine("WaitOne succeeded.");
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitOne." +
                "\r\n\tMessage: {0}", ex.Message);
        }
        finally
        {
            // Whether or not the exception was thrown, the current
            // thread owns the mutex, and must release it.
            //
            _orphan1.ReleaseMutex();
        }

        // Create an array of wait handles, consisting of one
        // ManualResetEvent and two mutexes, using two more of the
        // abandoned mutexes.
        WaitHandle[] waitFor = {_dummy, _orphan2, _orphan3};

        // WaitAny returns when any of the wait handles in the 
        // array is signaled, so either of the two abandoned mutexes
        // satisfy its wait condition. On returning from the wait,
        // WaitAny throws AbandonedMutexException. The MutexIndex
        // property returns the lower of the two index values for 
        // the abandoned mutexes. Note that the Try block and the
        // Catch block obtain the index in different ways.
        //  
        try
        {
            int index = WaitHandle.WaitAny(waitFor);
            Console.WriteLine("WaitAny succeeded.");

            // The current thread owns the mutex, and must release
            // it.
            Mutex m = waitFor[index] as Mutex;
            if (m != null) m.ReleaseMutex();
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitAny at index {0}." +
                "\r\n\tMessage: {1}", ex.MutexIndex, ex.Message);

            // Whether or not the exception was thrown, the current
            // thread owns the mutex, and must release it.
            //
            if (ex.Mutex != null) ex.Mutex.ReleaseMutex();
        }

        // Use two more of the abandoned mutexes for the WaitAll call.
        // WaitAll doesn't return until all wait handles are signaled,
        // so the ManualResetEvent must be signaled by calling Set().
        _dummy.Set();
        waitFor[1] = _orphan4;
        waitFor[2] = _orphan5;

        // The signaled event and the two abandoned mutexes satisfy
        // the wait condition for WaitAll, but on return it throws
        // AbandonedMutexException. For WaitAll, the MutexIndex
        // property is always -1 and the Mutex property is always
        // null.
        //  
        try
        {
            WaitHandle.WaitAll(waitFor);
            Console.WriteLine("WaitAll succeeded.");
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitAll. MutexIndex = {0}." +
                "\r\n\tMessage: {1}", ex.MutexIndex, ex.Message);
        }
        finally
        {
            // Whether or not the exception was thrown, the current
            // thread owns the mutexes, and must release them.
            //
            _orphan4.ReleaseMutex();
            _orphan5.ReleaseMutex();
        }
    }

    [MTAThread]
    public static void AbandonMutex()
    {
        _orphan1.WaitOne();
        _orphan2.WaitOne();
        _orphan3.WaitOne();
        _orphan4.WaitOne();
        _orphan5.WaitOne();
        // Abandon the mutexes by exiting without releasing them.
        Console.WriteLine("Thread exits without releasing the mutexes.");
    }
}

/* This code example produces the following output:

Thread exits without releasing the mutexes.
Exception on return from WaitOne.
        Message: The wait completed due to an abandoned mutex.
Exception on return from WaitAny at index 1.
        Message: The wait completed due to an abandoned mutex.
Exception on return from WaitAll. MutexIndex = -1.
        Message: The wait completed due to an abandoned mutex.
 */
Option Explicit
Imports System.Threading

Public Class Example
    Private Shared _dummy As New ManualResetEvent(False)

    Private Shared _orphan1 As New Mutex()
    Private Shared _orphan2 As New Mutex()
    Private Shared _orphan3 As New Mutex()
    Private Shared _orphan4 As New Mutex()
    Private Shared _orphan5 As New Mutex()

    <MTAThread> _
    Public Shared Sub Main()
        ' Start a thread that takes all five mutexes, and then
        ' ends without releasing them.
        '
        Dim t As New Thread(AddressOf AbandonMutex)
        t.Start()
        ' Make sure the thread is finished.
        t.Join()

        ' Wait on one of the abandoned mutexes. The WaitOne returns
        ' immediately, because its wait condition is satisfied by
        ' the abandoned mutex, but on return it throws
        ' AbandonedMutexException.
        Try
            _orphan1.WaitOne()
            Console.WriteLine("WaitOne succeeded.")
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitOne." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 
        Finally
            ' Whether or not the exception was thrown, the current
            ' thread owns the mutex, and must release it.
            '
            _orphan1.ReleaseMutex()
        End Try

        ' Create an array of wait handles, consisting of one
        ' ManualResetEvent and two mutexes, using two more of the
        ' abandoned mutexes.
        Dim waitFor(2) As WaitHandle
        waitFor(0) = _dummy
        waitFor(1) = _orphan2
        waitFor(2) = _orphan3

        ' WaitAny returns when any of the wait handles in the 
        ' array is signaled, so either of the two abandoned mutexes
        ' satisfy its wait condition. On returning from the wait,
        ' WaitAny throws AbandonedMutexException. The MutexIndex
        ' property returns the lower of the two index values for 
        ' the abandoned mutexes. Note that the Try block and the
        ' Catch block obtain the index in different ways.
        '  
        Try
            Dim index As Integer = WaitHandle.WaitAny(waitFor)
            Console.WriteLine("WaitAny succeeded.")

            Dim m As Mutex = TryCast(waitFor(index), Mutex)

            ' The current thread owns the mutex, and must release
            ' it.
            If m IsNot Nothing Then m.ReleaseMutex()
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitAny at index " _
                & ex.MutexIndex & "." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 

            ' Whether or not the exception was thrown, the current
            ' thread owns the mutex, and must release it.
            '
            If ex.Mutex IsNot Nothing Then ex.Mutex.ReleaseMutex()            
        End Try

        ' Use two more of the abandoned mutexes for the WaitAll call.
        ' WaitAll doesn't return until all wait handles are signaled,
        ' so the ManualResetEvent must be signaled by calling Set(). 
        _dummy.Set()
        waitFor(1) = _orphan4
        waitFor(2) = _orphan5

        ' The signaled event and the two abandoned mutexes satisfy
        ' the wait condition for WaitAll, but on return it throws
        ' AbandonedMutexException. For WaitAll, the MutexIndex
        ' property is always -1 and the Mutex property is always
        ' Nothing.
        '  
        Try
            WaitHandle.WaitAll(waitFor)
            Console.WriteLine("WaitAll succeeded.")
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitAll. MutexIndex = " _
                & ex.MutexIndex & "." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 
        Finally
            ' Whether or not the exception was thrown, the current
            ' thread owns the mutexes, and must release them.
            '
            CType(waitFor(1), Mutex).ReleaseMutex()
            CType(waitFor(2), Mutex).ReleaseMutex()
        End Try
    End Sub

    <MTAThread> _
    Public Shared Sub AbandonMutex()
        _orphan1.WaitOne()
        _orphan2.WaitOne()
        _orphan3.WaitOne()
        _orphan4.WaitOne()
        _orphan5.WaitOne()
        ' Abandon the mutexes by exiting without releasing them.
        Console.WriteLine("Thread exits without releasing the mutexes.")
    End Sub
End Class

' This code example produces the following output:
'
'Thread exits without releasing the mutexes.
'Exception on return from WaitOne.
'        Message: The wait completed due to an abandoned mutex.
'Exception on return from WaitAny at index 1.
'        Message: The wait completed due to an abandoned mutex.
'Exception on return from WaitAll. MutexIndex = -1.
'        Message: The wait completed due to an abandoned mutex.

설명

스레드가 뮤텍스를 중단하면 뮤텍스를 획득하는 다음 스레드에서 예외가 throw됩니다. 스레드는 뮤텍스에서 이미 대기 중이거나 나중에 뮤텍스에 진입하기 때문에 뮤텍스를 획득할 수 있습니다.

중단된 뮤텍스는 심각한 프로그래밍 오류를 나타냅니다. 뮤텍스를 해제하지 않고 스레드가 종료되면 뮤텍스로 보호되는 데이터 구조가 일관된 상태가 아닐 수 있습니다. .NET Framework 버전 2.0 이전에는 중단된 뮤텍스의 결과로 대기가 완료된 경우 예외가 throw되지 않아 이러한 문제를 검색하기 어려웠습니다. 자세한 내용은 Mutex 클래스를 참조하세요.

뮤텍스의 소유권을 요청하는 다음 스레드는 데이터 구조의 무결성을 확인할 수 있는 경우 이 예외를 처리하고 계속 진행할 수 있습니다.

생성자

AbandonedMutexException()

기본값을 사용하여 AbandonedMutexException 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(Int32, WaitHandle)

중단된 뮤텍스의 지정된 인덱스 및 뮤텍스를 나타내는 AbandonedMutexException 개체(해당 사항이 있을 경우)를 사용하여 Mutex 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(SerializationInfo, StreamingContext)

serialize된 데이터를 사용하여 AbandonedMutexException 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(String)

지정된 오류 메시지를 사용하여 AbandonedMutexException 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(String, Exception)

지정된 오류 메시지와 내부 예외를 사용하여 AbandonedMutexException 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(String, Exception, Int32, WaitHandle)

지정된 오류 메시지, 내부 예외, 중단된 뮤텍스의 인덱스 및 뮤텍스를 나타내는 AbandonedMutexException 개체(해당 사항이 있을 경우)를 사용하여 Mutex 클래스의 새 인스턴스를 초기화합니다.

AbandonedMutexException(String, Int32, WaitHandle)

지정된 오류 메시지, 중단된 뮤텍스의 인덱스 및 중단된 뮤텍스(해당 사항이 있을 경우)를 사용하여 AbandonedMutexException 클래스의 새 인스턴스를 초기화합니다.

속성

Data

예외에 대한 사용자 정의 정보를 추가로 제공하는 키/값 쌍 컬렉션을 가져옵니다.

(다음에서 상속됨 Exception)
HelpLink

이 예외와 연결된 도움말 파일에 대한 링크를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
HResult

특정 예외에 할당된 코드화된 숫자 값인 HRESULT를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
InnerException

현재 예외를 발생시킨 Exception 인스턴스를 가져옵니다.

(다음에서 상속됨 Exception)
Message

현재 예외를 설명하는 메시지를 가져옵니다.

(다음에서 상속됨 Exception)
Mutex

예외의 발생시킨 중단된 뮤텍스를 가져옵니다.

MutexIndex

예외의 발생시킨 중단된 뮤텍스를 가져옵니다.

Source

오류를 발생시키는 애플리케이션 또는 개체의 이름을 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
StackTrace

호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다.

(다음에서 상속됨 Exception)
TargetSite

현재 예외를 throw하는 메서드를 가져옵니다.

(다음에서 상속됨 Exception)

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetBaseException()

파생 클래스에서 재정의된 경우 하나 이상의 후속 예외의 근본 원인이 되는 Exception 을 반환합니다.

(다음에서 상속됨 Exception)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetObjectData(SerializationInfo, StreamingContext)

파생 클래스에서 재정의된 경우 예외에 관한 정보를 SerializationInfo 에 설정합니다.

(다음에서 상속됨 Exception)
GetType()

현재 인스턴스의 런타임 형식을 가져옵니다.

(다음에서 상속됨 Exception)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 예외에 대한 문자열 표현을 만들고 반환합니다.

(다음에서 상속됨 Exception)

이벤트

SerializeObjectState
사용되지 않습니다.

예외에 대한 serialize된 데이터가 들어 있는 예외 상태 개체가 만들어지도록 예외가 serialize될 때 발생합니다.

(다음에서 상속됨 Exception)

적용 대상

추가 정보