Semaphore.Release 메서드

정의

세마포를 종료합니다.Exits the semaphore.

오버로드

Release()

세마포를 종료하고 이전 카운트를 반환합니다.Exits the semaphore and returns the previous count.

Release(Int32)

지정된 횟수만큼 세마포를 종료하고 이전 카운트를 반환합니다.Exits the semaphore a specified number of times and returns the previous count.

Release()

세마포를 종료하고 이전 카운트를 반환합니다.Exits the semaphore and returns the previous count.

public:
 int Release();
public int Release ();
member this.Release : unit -> int
Public Function Release () As Integer

반환

Int32

Release 메서드가 호출되기 전의 세마포 카운트입니다.The count on the semaphore before the Release method was called.

예외

세마포 카운트가 이미 최대값인 경우The semaphore count is already at the maximum value.

명명된 세마포에서 Win32 오류가 발생한 경우A Win32 error occurred with a named semaphore.

현재 세마포가 명명된 시스템 세마포를 나타내지만 사용자에게 Modify가 없는 경우The current semaphore represents a named system semaphore, but the user does not have Modify.

-또는--or- 현재 세마포가 명명된 시스템 세마포를 나타내지만 세마포가 Modify를 사용하여 열리지 않은 경우The current semaphore represents a named system semaphore, but it was not opened with Modify.

예제

다음 코드 예제에서는 최대 세 가지 및 초기 카운트 0의 세마포를 만듭니다.The following code example creates a semaphore with a maximum count of three and an initial count of zero. 이 예제에서는 세마포 대기를 차단 하는 5 개의 스레드를 시작 합니다.The example starts five threads, which block waiting for the semaphore. 주 스레드는 메서드 오버 로드를 사용 하 여 Release(Int32) 세마포 수를 최대값으로 늘려 세 스레드를 세마포에 입력할 수 있도록 합니다.The main thread uses the Release(Int32) method overload to increase the semaphore count to its maximum, allowing three threads to enter the semaphore. 각 스레드는 메서드를 사용 하 여 Thread.Sleep 1 초 동안 대기한 다음 작업을 시뮬레이션 하 고 메서드 오버 로드를 호출 하 여 Release() 세마포를 해제 합니다.Each thread uses the Thread.Sleep method to wait for one second, to simulate work, and then calls the Release() method overload to release the semaphore.

세마포가 해제 될 때마다 이전 세마포 수가 표시 됩니다.Each time the semaphore is released, the previous semaphore count is displayed. 콘솔 메시지는 세마포 사용을 추적 합니다.Console messages track semaphore use. 시뮬레이션 된 작업 간격은 각 스레드에 대해 약간 증가 하 여 출력을 보다 쉽게 읽을 수 있도록 합니다.The simulated work interval is increased slightly for each thread, to make the output easier to read.

#using <System.dll>
using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
   // A semaphore that simulates a limited resource pool.
   //
   static Semaphore^ _pool;

   // A padding interval to make the output more orderly.
   static int _padding;

public:
   static void Main()
   {
      // Create a semaphore that can satisfy up to three
      // concurrent requests. Use an initial count of zero,
      // so that the entire semaphore count is initially
      // owned by the main program thread.
      //
      _pool = gcnew Semaphore( 0,3 );
      
      // Create and start five numbered threads.
      //
      for ( int i = 1; i <= 5; i++ )
      {
         Thread^ t = gcnew Thread(
            gcnew ParameterizedThreadStart( Worker ) );
         
         // Start the thread, passing the number.
         //
         t->Start( i );
      }
      
      // Wait for half a second, to allow all the
      // threads to start and to block on the semaphore.
      //
      Thread::Sleep( 500 );
      
      // The main thread starts out holding the entire
      // semaphore count. Calling Release(3) brings the
      // semaphore count back to its maximum value, and
      // allows the waiting threads to enter the semaphore,
      // up to three at a time.
      //
      Console::WriteLine( L"Main thread calls Release(3)." );
      _pool->Release( 3 );

      Console::WriteLine( L"Main thread exits." );
   }

private:
   static void Worker( Object^ num )
   {
      // Each worker thread begins by requesting the
      // semaphore.
      Console::WriteLine( L"Thread {0} begins and waits for the semaphore.", num );
      _pool->WaitOne();
      
      // A padding interval to make the output more orderly.
      int padding = Interlocked::Add( _padding, 100 );

      Console::WriteLine( L"Thread {0} enters the semaphore.", num );
      
      // The thread's "work" consists of sleeping for
      // about a second. Each thread "works" a little
      // longer, just to make the output more orderly.
      //
      Thread::Sleep( 1000 + padding );

      Console::WriteLine( L"Thread {0} releases the semaphore.", num );
      Console::WriteLine( L"Thread {0} previous semaphore count: {1}",
         num, _pool->Release() );
   }
};
using System;
using System.Threading;

public class Example
{
    // A semaphore that simulates a limited resource pool.
    //
    private static Semaphore _pool;

    // A padding interval to make the output more orderly.
    private static int _padding;

    public static void Main()
    {
        // Create a semaphore that can satisfy up to three
        // concurrent requests. Use an initial count of zero,
        // so that the entire semaphore count is initially
        // owned by the main program thread.
        //
        _pool = new Semaphore(0, 3);

        // Create and start five numbered threads. 
        //
        for(int i = 1; i <= 5; i++)
        {
            Thread t = new Thread(new ParameterizedThreadStart(Worker));

            // Start the thread, passing the number.
            //
            t.Start(i);
        }

        // Wait for half a second, to allow all the
        // threads to start and to block on the semaphore.
        //
        Thread.Sleep(500);

        // The main thread starts out holding the entire
        // semaphore count. Calling Release(3) brings the 
        // semaphore count back to its maximum value, and
        // allows the waiting threads to enter the semaphore,
        // up to three at a time.
        //
        Console.WriteLine("Main thread calls Release(3).");
        _pool.Release(3);

        Console.WriteLine("Main thread exits.");
    }

    private static void Worker(object num)
    {
        // Each worker thread begins by requesting the
        // semaphore.
        Console.WriteLine("Thread {0} begins " +
            "and waits for the semaphore.", num);
        _pool.WaitOne();

        // A padding interval to make the output more orderly.
        int padding = Interlocked.Add(ref _padding, 100);

        Console.WriteLine("Thread {0} enters the semaphore.", num);
        
        // The thread's "work" consists of sleeping for 
        // about a second. Each thread "works" a little 
        // longer, just to make the output more orderly.
        //
        Thread.Sleep(1000 + padding);

        Console.WriteLine("Thread {0} releases the semaphore.", num);
        Console.WriteLine("Thread {0} previous semaphore count: {1}",
            num, _pool.Release());
    }
}
Imports System.Threading

Public Class Example

    ' A semaphore that simulates a limited resource pool.
    '
    Private Shared _pool As Semaphore

    ' A padding interval to make the output more orderly.
    Private Shared _padding As Integer

    <MTAThread> _
    Public Shared Sub Main()
        ' Create a semaphore that can satisfy up to three
        ' concurrent requests. Use an initial count of zero,
        ' so that the entire semaphore count is initially
        ' owned by the main program thread.
        '
        _pool = New Semaphore(0, 3)

        ' Create and start five numbered threads. 
        '
        For i As Integer = 1 To 5
            Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker))
            'Dim t As New Thread(AddressOf Worker)

            ' Start the thread, passing the number.
            '
            t.Start(i)
        Next i

        ' Wait for half a second, to allow all the
        ' threads to start and to block on the semaphore.
        '
        Thread.Sleep(500)

        ' The main thread starts out holding the entire
        ' semaphore count. Calling Release(3) brings the 
        ' semaphore count back to its maximum value, and
        ' allows the waiting threads to enter the semaphore,
        ' up to three at a time.
        '
        Console.WriteLine("Main thread calls Release(3).")
        _pool.Release(3)

        Console.WriteLine("Main thread exits.")
    End Sub

    Private Shared Sub Worker(ByVal num As Object)
        ' Each worker thread begins by requesting the
        ' semaphore.
        Console.WriteLine("Thread {0} begins " _
            & "and waits for the semaphore.", num)
        _pool.WaitOne()

        ' A padding interval to make the output more orderly.
        Dim padding As Integer = Interlocked.Add(_padding, 100)

        Console.WriteLine("Thread {0} enters the semaphore.", num)
        
        ' The thread's "work" consists of sleeping for 
        ' about a second. Each thread "works" a little 
        ' longer, just to make the output more orderly.
        '
        Thread.Sleep(1000 + padding)

        Console.WriteLine("Thread {0} releases the semaphore.", num)
        Console.WriteLine("Thread {0} previous semaphore count: {1}", _
            num, _
            _pool.Release())
    End Sub
End Class

설명

일반적으로 스레드는 WaitOne 메서드를 사용 하 여 세마포를 입력 하 고, 일반적으로이 메서드 오버 로드를 사용 하 여 종료 합니다.Threads typically use the WaitOne method to enter the semaphore, and they typically use this method overload to exit.

SemaphoreFullException이 메서드에 의해 throw 되는 경우에는 Release 호출 스레드에 문제가 있음을 나타내는 것은 아닙니다.If a SemaphoreFullException is thrown by the Release method, it does not necessarily indicate a problem with the calling thread. 다른 스레드에서 프로그래밍 오류가 발생 하 여 해당 스레드가 입력 한 것 보다 많은 횟수로 세마포를 종료 했을 수 있습니다.A programming error in another thread might have caused that thread to exit the semaphore more times than it entered.

현재 Semaphore 개체가 명명 된 시스템 세마포를 나타내는 경우 사용자에 게 권한이 있어야 SemaphoreRights.Modify 하 고 해당 세마포가 권한을 사용 하 여 열려야 합니다 SemaphoreRights.Modify .If the current Semaphore object represents a named system semaphore, the user must have SemaphoreRights.Modify rights and the semaphore must have been opened with SemaphoreRights.Modify rights.

추가 정보

적용 대상

Release(Int32)

지정된 횟수만큼 세마포를 종료하고 이전 카운트를 반환합니다.Exits the semaphore a specified number of times and returns the previous count.

public:
 int Release(int releaseCount);
public int Release (int releaseCount);
member this.Release : int -> int
Public Function Release (releaseCount As Integer) As Integer

매개 변수

releaseCount
Int32

세마포를 종료할 횟수입니다.The number of times to exit the semaphore.

반환

Int32

Release 메서드가 호출되기 전의 세마포 카운트입니다.The count on the semaphore before the Release method was called.

예외

releaseCount가 1보다 작습니다.releaseCount is less than 1.

세마포 카운트가 이미 최대값인 경우The semaphore count is already at the maximum value.

명명된 세마포에서 Win32 오류가 발생한 경우A Win32 error occurred with a named semaphore.

현재 세마포가 명명된 시스템 세마포를 나타내지만 사용자에게 Modify 권한이 없는 경우The current semaphore represents a named system semaphore, but the user does not have Modify rights.

-또는--or- 현재 세마포가 명명된 시스템 세마포를 나타내지만 세마포가 Modify 권한을 사용하여 열리지 않은 경우The current semaphore represents a named system semaphore, but it was not opened with Modify rights.

예제

다음 코드 예제에서는 최대 세 가지 및 초기 카운트 0의 세마포를 만듭니다.The following code example creates a semaphore with a maximum count of three and an initial count of zero. 이 예제에서는 세마포 대기를 차단 하는 5 개의 스레드를 시작 합니다.The example starts five threads, which block waiting for the semaphore. 주 스레드는 메서드 오버 로드를 사용 하 여 Release(Int32) 세마포 수를 최대값으로 늘려 세 스레드를 세마포에 입력할 수 있도록 합니다.The main thread uses the Release(Int32) method overload to increase the semaphore count to its maximum, allowing three threads to enter the semaphore. 각 스레드는 메서드를 사용 하 여 Thread.Sleep 1 초 동안 대기한 다음 작업을 시뮬레이션 하 고 메서드 오버 로드를 호출 하 여 Release() 세마포를 해제 합니다.Each thread uses the Thread.Sleep method to wait for one second, to simulate work, and then calls the Release() method overload to release the semaphore.

세마포가 해제 될 때마다 이전 세마포 수가 표시 됩니다.Each time the semaphore is released, the previous semaphore count is displayed. 콘솔 메시지는 세마포 사용을 추적 합니다.Console messages track semaphore use. 시뮬레이션 된 작업 간격은 각 스레드에 대해 약간 증가 하 여 출력을 보다 쉽게 읽을 수 있도록 합니다.The simulated work interval is increased slightly for each thread, to make the output easier to read.

#using <System.dll>
using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
   // A semaphore that simulates a limited resource pool.
   //
   static Semaphore^ _pool;

   // A padding interval to make the output more orderly.
   static int _padding;

public:
   static void Main()
   {
      // Create a semaphore that can satisfy up to three
      // concurrent requests. Use an initial count of zero,
      // so that the entire semaphore count is initially
      // owned by the main program thread.
      //
      _pool = gcnew Semaphore( 0,3 );
      
      // Create and start five numbered threads.
      //
      for ( int i = 1; i <= 5; i++ )
      {
         Thread^ t = gcnew Thread(
            gcnew ParameterizedThreadStart( Worker ) );
         
         // Start the thread, passing the number.
         //
         t->Start( i );
      }
      
      // Wait for half a second, to allow all the
      // threads to start and to block on the semaphore.
      //
      Thread::Sleep( 500 );
      
      // The main thread starts out holding the entire
      // semaphore count. Calling Release(3) brings the
      // semaphore count back to its maximum value, and
      // allows the waiting threads to enter the semaphore,
      // up to three at a time.
      //
      Console::WriteLine( L"Main thread calls Release(3)." );
      _pool->Release( 3 );

      Console::WriteLine( L"Main thread exits." );
   }

private:
   static void Worker( Object^ num )
   {
      // Each worker thread begins by requesting the
      // semaphore.
      Console::WriteLine( L"Thread {0} begins and waits for the semaphore.", num );
      _pool->WaitOne();
      
      // A padding interval to make the output more orderly.
      int padding = Interlocked::Add( _padding, 100 );

      Console::WriteLine( L"Thread {0} enters the semaphore.", num );
      
      // The thread's "work" consists of sleeping for
      // about a second. Each thread "works" a little
      // longer, just to make the output more orderly.
      //
      Thread::Sleep( 1000 + padding );

      Console::WriteLine( L"Thread {0} releases the semaphore.", num );
      Console::WriteLine( L"Thread {0} previous semaphore count: {1}",
         num, _pool->Release() );
   }
};
using System;
using System.Threading;

public class Example
{
    // A semaphore that simulates a limited resource pool.
    //
    private static Semaphore _pool;

    // A padding interval to make the output more orderly.
    private static int _padding;

    public static void Main()
    {
        // Create a semaphore that can satisfy up to three
        // concurrent requests. Use an initial count of zero,
        // so that the entire semaphore count is initially
        // owned by the main program thread.
        //
        _pool = new Semaphore(0, 3);

        // Create and start five numbered threads. 
        //
        for(int i = 1; i <= 5; i++)
        {
            Thread t = new Thread(new ParameterizedThreadStart(Worker));

            // Start the thread, passing the number.
            //
            t.Start(i);
        }

        // Wait for half a second, to allow all the
        // threads to start and to block on the semaphore.
        //
        Thread.Sleep(500);

        // The main thread starts out holding the entire
        // semaphore count. Calling Release(3) brings the 
        // semaphore count back to its maximum value, and
        // allows the waiting threads to enter the semaphore,
        // up to three at a time.
        //
        Console.WriteLine("Main thread calls Release(3).");
        _pool.Release(3);

        Console.WriteLine("Main thread exits.");
    }

    private static void Worker(object num)
    {
        // Each worker thread begins by requesting the
        // semaphore.
        Console.WriteLine("Thread {0} begins " +
            "and waits for the semaphore.", num);
        _pool.WaitOne();

        // A padding interval to make the output more orderly.
        int padding = Interlocked.Add(ref _padding, 100);

        Console.WriteLine("Thread {0} enters the semaphore.", num);
        
        // The thread's "work" consists of sleeping for 
        // about a second. Each thread "works" a little 
        // longer, just to make the output more orderly.
        //
        Thread.Sleep(1000 + padding);

        Console.WriteLine("Thread {0} releases the semaphore.", num);
        Console.WriteLine("Thread {0} previous semaphore count: {1}",
            num, _pool.Release());
    }
}
Imports System.Threading

Public Class Example

    ' A semaphore that simulates a limited resource pool.
    '
    Private Shared _pool As Semaphore

    ' A padding interval to make the output more orderly.
    Private Shared _padding As Integer

    <MTAThread> _
    Public Shared Sub Main()
        ' Create a semaphore that can satisfy up to three
        ' concurrent requests. Use an initial count of zero,
        ' so that the entire semaphore count is initially
        ' owned by the main program thread.
        '
        _pool = New Semaphore(0, 3)

        ' Create and start five numbered threads. 
        '
        For i As Integer = 1 To 5
            Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker))
            'Dim t As New Thread(AddressOf Worker)

            ' Start the thread, passing the number.
            '
            t.Start(i)
        Next i

        ' Wait for half a second, to allow all the
        ' threads to start and to block on the semaphore.
        '
        Thread.Sleep(500)

        ' The main thread starts out holding the entire
        ' semaphore count. Calling Release(3) brings the 
        ' semaphore count back to its maximum value, and
        ' allows the waiting threads to enter the semaphore,
        ' up to three at a time.
        '
        Console.WriteLine("Main thread calls Release(3).")
        _pool.Release(3)

        Console.WriteLine("Main thread exits.")
    End Sub

    Private Shared Sub Worker(ByVal num As Object)
        ' Each worker thread begins by requesting the
        ' semaphore.
        Console.WriteLine("Thread {0} begins " _
            & "and waits for the semaphore.", num)
        _pool.WaitOne()

        ' A padding interval to make the output more orderly.
        Dim padding As Integer = Interlocked.Add(_padding, 100)

        Console.WriteLine("Thread {0} enters the semaphore.", num)
        
        ' The thread's "work" consists of sleeping for 
        ' about a second. Each thread "works" a little 
        ' longer, just to make the output more orderly.
        '
        Thread.Sleep(1000 + padding)

        Console.WriteLine("Thread {0} releases the semaphore.", num)
        Console.WriteLine("Thread {0} previous semaphore count: {1}", _
            num, _
            _pool.Release())
    End Sub
End Class

설명

스레드가 세마포를 여러 번 입력 한 경우이 메서드 오버 로드를 사용 하면 한 번의 호출로 전체 세마포 수를 복원할 수 있습니다.If a thread has entered the semaphore multiple times, this method overload allows the entire semaphore count to be restored with one call.

SemaphoreFullException이 메서드에 의해 throw 되는 경우에는 Release 호출 스레드에 문제가 있음을 나타내는 것은 아닙니다.If a SemaphoreFullException is thrown by the Release method, it does not necessarily indicate a problem with the calling thread. 다른 스레드에서 프로그래밍 오류가 발생 하 여 해당 스레드가 입력 한 것 보다 많은 횟수로 세마포를 종료 했을 수 있습니다.A programming error in another thread might have caused that thread to exit the semaphore more times than it entered.

현재 Semaphore 개체가 명명 된 시스템 세마포를 나타내는 경우 사용자에 게 권한이 있어야 SemaphoreRights.Modify 하 고 해당 세마포가 권한을 사용 하 여 열려야 합니다 SemaphoreRights.Modify .If the current Semaphore object represents a named system semaphore, the user must have SemaphoreRights.Modify rights and the semaphore must have been opened with SemaphoreRights.Modify rights.

추가 정보

적용 대상