Semaphore.Release Semaphore.Release Semaphore.Release Semaphore.Release Method

定義

セマフォから出ます。Exits the semaphore.

オーバーロード

Release() Release() Release() Release()

セマフォから出て、前のカウントを返します。Exits the semaphore and returns the previous count.

Release(Int32) Release(Int32) Release(Int32) Release(Int32)

指定した回数だけセマフォから出て、前のカウントを返します。Exits the semaphore a specified number of times and returns the previous count.

Release() Release() Release() 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

戻り値

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 の最初の数の 3 つの最大数、およびセマフォを作成します。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)メソッドのオーバー ロードは、次の 3 つのスレッドがセマフォに入るをできるように、最大容量にセマフォ カウントを増やします。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
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によってスローされる、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) Release(Int32) Release(Int32) 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 Int32 Int32 Int32

セマフォから出る回数。The number of times to exit the semaphore.

戻り値

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 rights.

または-or- 現在のセマフォは名前付きシステム セマフォを表していますが、Modify 権限で開かれませんでした。The current semaphore represents a named system semaphore, but it was not opened with Modify rights.

次のコード例は、0 の最初の数の 3 つの最大数、およびセマフォを作成します。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)メソッドのオーバー ロードは、次の 3 つのスレッドがセマフォに入るをできるように、最大容量にセマフォ カウントを増やします。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
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

注釈

スレッドは、セマフォを複数回入力が、このメソッドのオーバー ロードでは、全体のセマフォのカウントを 1 回の呼び出しで復元するができます。If a thread has entered the semaphore multiple times, this method overload allows the entire semaphore count to be restored with one call.

場合、SemaphoreFullExceptionによってスローされる、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.

こちらもご覧ください

適用対象