Semaphore.Release Metoda

Definicja

Zamyka semafor.

Przeciążenia

Nazwa Opis
Release()

Zamyka semafor i zwraca poprzednią liczbę.

Release(Int32)

Zamyka semafor określoną liczbę razy i zwraca poprzednią liczbę.

Release()

Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs

Zamyka semafor i zwraca poprzednią liczbę.

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

Zwraca

Liczba semaforów przed wywołaniem Release metody.

Wyjątki

Liczba semaforów jest już na maksymalnej wartości.

Wystąpił błąd Win32 z nazwanym semaforem.

Bieżący semafor reprezentuje nazwany semafor systemowy, ale użytkownik nie ma Modifyelementu .

— lub —

Bieżący semafor reprezentuje nazwany semafor systemowy, ale nie został otwarty za pomocą Modifypolecenia .

Przykłady

Poniższy przykład kodu tworzy semafor z maksymalną liczbą trzech i początkową liczbą zera. W przykładzie uruchamia się pięć wątków, które blokują oczekiwanie na semafor. Główny wątek używa Release(Int32) przeciążenia metody, aby zwiększyć liczbę semaforów do maksymalnej, umożliwiając trzy wątki wejścia semafora. Każdy wątek używa Thread.Sleep metody , aby poczekać na jedną sekundę, zasymulować pracę, a następnie wywołuje Release() przeciążenie metody w celu zwolnienia semafora.

Za każdym razem, gdy semafor jest zwalniany, wyświetlana jest poprzednia liczba semaforów. Komunikaty konsoli śledzą użycie semafora. Symulowany interwał pracy jest nieznacznie zwiększany dla każdego wątku, aby ułatwić odczytywanie danych wyjściowych.

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

Uwagi

Wątki zwykle używają WaitOne metody do wprowadzania semafora i zwykle używają tego przeciążenia metody do zakończenia.

Jeśli element SemaphoreFullException jest zgłaszany przez metodę Release , nie musi wskazywać problemu z wątkiem wywołującym. Błąd programowania w innym wątku mógł spowodować, że wątek zamknąć semafor więcej razy niż wprowadzono.

Jeśli bieżący Semaphore obiekt reprezentuje nazwany semafor systemowy, użytkownik musi mieć SemaphoreRights.Modify prawa, a semafor musi zostać otwarty z prawami SemaphoreRights.Modify .

Zobacz też

Dotyczy

Release(Int32)

Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs
Źródło:
Semaphore.cs

Zamyka semafor określoną liczbę razy i zwraca poprzednią liczbę.

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

Parametry

releaseCount
Int32

Liczba przypadków wyjścia z semafora.

Zwraca

Liczba semaforów przed wywołaniem Release metody.

Wyjątki

releaseCount wartość jest mniejsza niż 1.

Liczba semaforów jest już na maksymalnej wartości.

Wystąpił błąd Win32 z nazwanym semaforem.

Bieżący semafor reprezentuje nazwany semafor systemowy, ale użytkownik nie ma Modify praw.

— lub —

Bieżący semafor reprezentuje nazwany semafor systemowy, ale nie został otwarty z prawami Modify .

Przykłady

Poniższy przykład kodu tworzy semafor z maksymalną liczbą trzech i początkową liczbą zera. W przykładzie uruchamia się pięć wątków, które blokują oczekiwanie na semafor. Główny wątek używa Release(Int32) przeciążenia metody, aby zwiększyć liczbę semaforów do maksymalnej, umożliwiając trzy wątki wejścia semafora. Każdy wątek używa Thread.Sleep metody , aby poczekać na jedną sekundę, zasymulować pracę, a następnie wywołuje Release() przeciążenie metody w celu zwolnienia semafora.

Za każdym razem, gdy semafor jest zwalniany, wyświetlana jest poprzednia liczba semaforów. Komunikaty konsoli śledzą użycie semafora. Symulowany interwał pracy jest nieznacznie zwiększany dla każdego wątku, aby ułatwić odczytywanie danych wyjściowych.

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

Uwagi

Jeśli wątek wprowadził semafor wiele razy, to przeciążenie metody umożliwia przywrócenie całej liczby semaforów za pomocą jednego wywołania.

Jeśli element SemaphoreFullException jest zgłaszany przez metodę Release , nie musi wskazywać problemu z wątkiem wywołującym. Błąd programowania w innym wątku mógł spowodować, że wątek zamknąć semafor więcej razy niż wprowadzono.

Jeśli bieżący Semaphore obiekt reprezentuje nazwany semafor systemowy, użytkownik musi mieć SemaphoreRights.Modify prawa, a semafor musi zostać otwarty z prawami SemaphoreRights.Modify .

Zobacz też

Dotyczy