Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Semaphore::Release Method (Int32)

Exits the semaphore a specified number of times and returns the previous count.

Namespace:  System.Threading
Assembly:  System (in System.dll)

int Release(
	int releaseCount


Type: System::Int32
The number of times to exit the semaphore.

Return Value

Type: System::Int32
The count on the semaphore before the Release method was called.


releaseCount is less than 1.


The semaphore count is already at the maximum value.


A Win32 error occurred with a named semaphore.


The current semaphore represents a named system semaphore, but the user does not have SemaphoreRights::Modify rights.


The current semaphore represents a named system semaphore, but it was not opened with SemaphoreRights::Modify rights.

If a thread has entered the semaphore multiple times, this method overload allows the entire semaphore count to be restored with one call.

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.

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.

The following code example creates a semaphore with a maximum count of three and an initial count of zero. The example starts five threads, which block waiting for the semaphore. The main thread uses the Release(Int32) method overload to increase the semaphore count to its maximum, allowing three threads to enter the semaphore. 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
   // A semaphore that simulates a limited resource pool.
   static Semaphore^ _pool;

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

   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." );

   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 );

      // 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() );

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2017 Microsoft