Semaphore Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Limits the number of threads that can access a resource or pool of resources concurrently.

Inheritance Hierarchy

System..::.Object
System.Threading..::.WaitHandle
System.Threading..::.Semaphore

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

Syntax

Public NotInheritable Class Semaphore _
    Inherits WaitHandle
public sealed class Semaphore : WaitHandle

The Semaphore type exposes the following members.

Constructors

Name Description
Semaphore(Int32, Int32) Initializes a new instance of the Semaphore class, specifying the maximum number of concurrent entries and optionally reserving some entries.
Semaphore(Int32, Int32, String) Security Critical. Initializes a new instance of the Semaphore class, specifying the maximum number of concurrent entries, optionally reserving some entries for the calling thread, and optionally specifying the name of a system semaphore object.
Semaphore(Int32, Int32, String, Boolean%) Security Critical. 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.

Top

Properties

Name Description
SafeWaitHandle Gets or sets the native operating-system handle. (Inherited from WaitHandle.)

Top

Methods

Name Description
Close When overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.)
Dispose()()() Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)
Dispose(Boolean) When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.)
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
OpenExisting Security Critical. Opens the specified named semaphore, if it already exists.
Release()()() Exits the semaphore and returns the previous count.
Release(Int32) Exits the semaphore a specified number of times and returns the previous count.
ToString Returns a string that represents the current object. (Inherited from Object.)
TryOpenExisting Security Critical. Opens the specified named semaphore, if it already exists, and returns a value that indicates whether the operation succeeded.
WaitOne()()() Blocks the current thread until the current WaitHandle receives a signal. (Inherited from WaitHandle.)
WaitOne(Int32) Blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to specify the time interval. (Inherited from WaitHandle.)
WaitOne(TimeSpan) Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)

Top

Remarks

Use the Semaphore class to control access to a pool of resources. Threads enter the semaphore by calling the WaitOne method, which is inherited from the WaitHandle class, and release the semaphore by calling the Release method.

The count on a semaphore is decremented each time a thread enters the semaphore, and incremented when a thread releases the semaphore. When the count is zero, subsequent requests block until other threads release the semaphore. When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created.

There is no guaranteed order, such as FIFO or LIFO, in which blocked threads enter the semaphore.

A thread can enter the semaphore multiple times, by calling the WaitOne method repeatedly. To release some or all of these entries, the thread can call the parameterless Release()()() method overload multiple times, or it can call the Release(Int32) method overload that specifies the number of entries to be released.

The Semaphore class does not enforce thread identity on calls to WaitOne or Release. It is the programmer's responsibility to ensure that threads do not release the semaphore too many times. For example, suppose a semaphore has a maximum count of two, and that thread A and thread B both enter the semaphore. If a programming error in thread B causes it to call Release twice, both calls succeed. The count on the semaphore is full, and when thread A eventually calls Release, a SemaphoreFullException is thrown.

Semaphores are of two types: local semaphores and named system semaphores. If you create a Semaphore object using a constructor that accepts a name, it is associated with an operating-system semaphore of that name. Named system semaphores are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create multiple Semaphore objects that represent the same named system semaphore, and you can use the OpenExisting method to open an existing named system semaphore.

A local semaphore exists only within your process. It can be used by any thread in your process that has a reference to the local Semaphore object. Each Semaphore object is a separate local semaphore.

Examples

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.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

Thread Safety

This type is thread safe.

See Also

Reference

System.Threading Namespace

Other Resources

Managed Threading

[7722a333-b974-47a2-a7c0-f09097fb644e]