Semaphore.Release 方法

定義

旗語從中走出。

多載

名稱 Description
Release()

退出信號板並返回先前的計數。

Release(Int32)

離開旗號指定次數並返回先前的計數。

Release()

來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs

退出信號板並返回先前的計數。

public:
 int Release();
public int Release();
member this.Release : unit -> int
Public Function Release () As Integer

傳回

在呼叫方法前 Release ,先在信號量上計數。

例外狀況

臂板數量已經達到最大值。

Win32 在命名旗語時發生錯誤。

目前的信號量代表一個命名系統的信號量,但使用者沒有 Modify

-或-

目前的臂板代表一個命名系統的旗號,但開頭時並非以 開頭。Modify

範例

以下程式碼範例建立一個最大計數為三、初始計數為零的信號量。 範例中起始五個執行緒,這些執行緒會阻塞等待信號板的出現。 主執行緒利用 Release(Int32) 方法超載將信號量數量提高到最大,允許三個執行緒進入信號量。 每個執行緒會使用該 Thread.Sleep 方法等待一秒,模擬工作,然後呼叫 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(initialCount: 0, maximumCount: 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(releaseCount: 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.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 方法輸入信號量,並通常利用此方法過載來退出。

如果方法拋出 SemaphoreFullException aRelease,並不一定代表呼叫執行緒有問題。 其他執行緒的程式錯誤可能導致該執行緒退出信號量的次數多於進入次數。

若當前 Semaphore 物件代表命名系統信號量,使用者必須擁有 SemaphoreRights.Modify 權限,且該信號量必須以權限開啟 SemaphoreRights.Modify

另請參閱

適用於

Release(Int32)

來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs
來源:
Semaphore.cs

離開旗號指定次數並返回先前的計數。

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

退出信號旗的次數。

傳回

在呼叫方法前 Release ,先在信號量上計數。

例外狀況

releaseCount 小於1。

臂板數量已經達到最大值。

Win32 在命名旗語時發生錯誤。

目前的旗號代表一個命名系統的旗號,但使用者沒有權利 Modify

-或-

現行的臂板代表一種命名系統的臂板,但當時並未獲得 Modify 授權。

範例

以下程式碼範例建立一個最大計數為三、初始計數為零的信號量。 範例中起始五個執行緒,這些執行緒會阻塞等待信號板的出現。 主執行緒利用 Release(Int32) 方法超載將信號量數量提高到最大,允許三個執行緒進入信號量。 每個執行緒會使用該 Thread.Sleep 方法等待一秒,模擬工作,然後呼叫 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(initialCount: 0, maximumCount: 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(releaseCount: 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.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

備註

若執行緒多次進入信號量,此方法過載允許一次呼叫恢復整個信號量計數。

如果方法拋出 SemaphoreFullException aRelease,並不一定代表呼叫執行緒有問題。 其他執行緒的程式錯誤可能導致該執行緒退出信號量的次數多於進入次數。

若當前 Semaphore 物件代表命名系統信號量,使用者必須擁有 SemaphoreRights.Modify 權限,且該信號量必須以權限開啟 SemaphoreRights.Modify

另請參閱

適用於