Export (0) Print
Expand All

Semaphore Constructor (Int32, Int32, String, Boolean%)

Initializes a new instance of the Semaphore class, specifying the maximum number of concurrent entries, optionally reserving some entries for the calling thread, optionally specifying the name of a system semaphore object, and specifying a variable that receives a value indicating whether a new system semaphore was created.

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

	int initialCount, 
	int maximumCount, 
	String^ name, 
	[OutAttribute] bool% createdNew


Type: System::Int32

The initial number of requests for the semaphore that can be satisfied concurrently.

Type: System::Int32

The maximum number of requests for the semaphore that can be satisfied concurrently.

Type: System::String

The name of a named system semaphore object.

Type: System::Boolean%

When this method returns, contains true if a local semaphore was created (that is, if name is nullptr or an empty string) or if the specified named system semaphore was created; false if the specified named system semaphore already existed. This parameter is passed uninitialized.


initialCount is greater than maximumCount.


name is longer than 260 characters.


maximumCount is less than 1.


initialCount is less than 0.


A Win32 error occurred.


The named semaphore exists and has access control security, and the user does not have SemaphoreRights::FullControl.


The named semaphore cannot be created, perhaps because a wait handle of a different type has the same name.

This constructor initializes a Semaphore object that represents a named system semaphore. You can create multiple Semaphore objects that represent the same named system semaphore.

If the named system semaphore does not exist, it is created with the initial count and maximum count specified by initialCount and maximumCount. If the named system semaphore already exists, initialCount and maximumCount are not used, although invalid values still cause exceptions. Use createdNew to determine whether the system semaphore was created.

If initialCount is less than maximumCount, and createdNew is true, the effect is the same as if the current thread had called WaitOne (maximumCount minus initialCount) times.

If you specify nullptr or an empty string for name, a local semaphore is created, as if you had called the Semaphore(Int32, Int32) constructor overload. In this case, createdNew is always true.

Because named semaphores are visible throughout the operating system, they can be used to coordinate resource use across process boundaries.

The following code example demonstrates the cross-process behavior of a named semaphore. The example creates a named semaphore with a maximum count of five and an initial count of two. That is, it reserves three entries for the thread that calls the constructor. If createNew is false, the program makes three calls to the WaitOne method. Thus, if you run the compiled example from two command windows, the second copy will block on the third call to WaitOne. Release one or more entries in the first copy of the program to unblock the second.

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

public ref class Example
   static void main()
      // The value of this variable is set by the semaphore 
      // constructor. It is true if the named system semaphore was 
      // created, and false if the named semaphore already existed. 
      bool semaphoreWasCreated;

      // Create a Semaphore object that represents the named 
      // system semaphore "SemaphoreExample". The semaphore has a
      // maximum count of five, and an initial count of two. The 
      // Boolean value that indicates creation of the underlying 
      // system object is placed in semaphoreWasCreated. 
      Semaphore^ sem = gcnew Semaphore( 2,5,L"SemaphoreExample",
         semaphoreWasCreated );
      if ( semaphoreWasCreated )
         // If the named system semaphore was created, its count is 
         // set to the initial count requested in the constructor. 
         // In effect, the current thread has entered the semaphore 
         // three times. 
         Console::WriteLine( L"Entered the semaphore three times." );
         // If the named system semaphore was not created, 
         // attempt to enter it three times. If another copy of 
         // this program is already running, only the first two 
         // requests can be satisfied. The third blocks. 
         Console::WriteLine( L"Entered the semaphore once." );
         Console::WriteLine( L"Entered the semaphore twice." );
         Console::WriteLine( L"Entered the semaphore three times." );

      // The thread executing this program has entered the 
      // semaphore three times. If a second copy of the program 
      // is run, it will block until this program releases the 
      // semaphore at least once. 
      Console::WriteLine( L"Enter the number of times to call Release." );
      int n;
      if ( Int32::TryParse( Console::ReadLine(), n ) )
         sem->Release( n );

      int remaining = 3 - n;
      if ( remaining > 0 )
         Console::WriteLine( L"Press Enter to release the remaining "
         L"count ({0}) and exit the program.", remaining );
         sem->Release( remaining );

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft