Export (0) Print
Expand All

Semaphore Constructor (Int32, Int32)

Initializes a new instance of the Semaphore class, specifying the maximum number of concurrent entries and optionally reserving some entries.

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

Public Sub New ( _
	initialCount As Integer, _
	maximumCount As Integer _


Type: System.Int32

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

Type: System.Int32

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


initialCount is greater than maximumCount.


maximumCount is less than 1.


initialCount is less than 0.

This constructor initializes an unnamed semaphore. All threads that use an instance of such a semaphore must have references to the instance.

If initialCount is less than maximumCount, the effect is the same as if the current thread had called WaitOne (maximumCount minus initialCount) times. If you do not want to reserve any entries for the thread that creates the semaphore, use the same number for maximumCount and initialCount.

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.

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. 
        Next i

        ' Wait for half a second, to allow all the 
        ' threads to start and to block on the semaphore. 

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

        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)

        ' 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, _
    End Sub 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 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 8, Silverlight 8.1

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