Semaphore.Release Methode

Definition

Beendet das Semaphor.

Überlädt

Name Beschreibung
Release()

Beendet das Semaphor und gibt die vorherige Anzahl zurück.

Release(Int32)

Beendet das Semaphor mit einer angegebenen Anzahl von Malen und gibt die vorherige Anzahl zurück.

Release()

Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs

Beendet das Semaphor und gibt die vorherige Anzahl zurück.

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

Gibt zurück

Die Anzahl des Semaphors, bevor die Release Methode aufgerufen wurde.

Ausnahmen

Die Semaphoranzahl ist bereits am Maximalwert.

Mit einem benannten Semaphor ist ein Win32-Fehler aufgetreten.

Der aktuelle Semaphor stellt einen benannten System-Semaphor dar, aber der Benutzer hat nicht Modify.

- oder -

Der aktuelle Semaphor stellt einen benannten System-Semaphor dar, aber es wurde nicht mit Modifygeöffnet.

Beispiele

Im folgenden Codebeispiel wird ein Semaphor mit einer maximalen Anzahl von drei und einer Anfangsanzahl von Null erstellt. Das Beispiel startet fünf Threads, die auf das Semaphor warten. Der Hauptthread verwendet die Release(Int32) Methodenüberladung, um die Semaphoranzahl auf sein Maximum zu erhöhen, sodass drei Threads in das Semaphor gelangen können. Jeder Thread verwendet die Thread.Sleep Methode, um eine Sekunde zu warten, um Arbeit zu simulieren, und ruft dann die Release() Methodenüberladung auf, um das Semaphor freizugeben.

Jedes Mal, wenn der Semaphor losgelassen wird, wird die vorherige Semaphoranzahl angezeigt. Konsolennachrichten verfolgen die Verwendung von Semaphoren. Das simulierte Arbeitsintervall wird für jeden Thread leicht erhöht, um die Ausgabe leichter lesbar zu machen.

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

Hinweise

Threads verwenden in der Regel die WaitOne Methode, um das Semaphor einzugeben, und sie verwenden diese Methodenüberladung in der Regel zum Beenden.

Wenn ein Fehler SemaphoreFullException durch die Release Methode ausgelöst wird, weist sie nicht unbedingt auf ein Problem mit dem aufrufenden Thread hin. Ein Programmierfehler in einem anderen Thread hat möglicherweise dazu geführt, dass der Thread das Semaphor mehrmals verlässt, als er eingegeben wurde.

Wenn das aktuelle Semaphore Objekt ein benanntes System-Semaphor darstellt, muss der Benutzer über Rechte verfügen SemaphoreRights.Modify , und das Semaphor muss mit SemaphoreRights.Modify Rechten geöffnet worden sein.

Weitere Informationen

Gilt für:

Release(Int32)

Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs
Quelle:
Semaphore.cs

Beendet das Semaphor mit einer angegebenen Anzahl von Malen und gibt die vorherige Anzahl zurück.

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

Parameter

releaseCount
Int32

Die Anzahl der Male, um das Semaphor zu verlassen.

Gibt zurück

Die Anzahl des Semaphors, bevor die Release Methode aufgerufen wurde.

Ausnahmen

releaseCount ist kleiner als 1.

Die Semaphoranzahl ist bereits am Maximalwert.

Mit einem benannten Semaphor ist ein Win32-Fehler aufgetreten.

Der aktuelle Semaphor stellt einen benannten System-Semaphor dar, der Benutzer hat jedoch keine Modify Rechte.

- oder -

Der aktuelle Semaphor stellt einen benannten System-Semaphor dar, wurde aber nicht mit Modify Rechten geöffnet.

Beispiele

Im folgenden Codebeispiel wird ein Semaphor mit einer maximalen Anzahl von drei und einer Anfangsanzahl von Null erstellt. Das Beispiel startet fünf Threads, die auf das Semaphor warten. Der Hauptthread verwendet die Release(Int32) Methodenüberladung, um die Semaphoranzahl auf sein Maximum zu erhöhen, sodass drei Threads in das Semaphor gelangen können. Jeder Thread verwendet die Thread.Sleep Methode, um eine Sekunde zu warten, um Arbeit zu simulieren, und ruft dann die Release() Methodenüberladung auf, um das Semaphor freizugeben.

Jedes Mal, wenn der Semaphor losgelassen wird, wird die vorherige Semaphoranzahl angezeigt. Konsolennachrichten verfolgen die Verwendung von Semaphoren. Das simulierte Arbeitsintervall wird für jeden Thread leicht erhöht, um die Ausgabe leichter lesbar zu machen.

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

Hinweise

Wenn ein Thread mehrmals in das Semaphor eingegeben wurde, kann mit dieser Methodenüberladung die gesamte Semaphoranzahl mit einem Aufruf wiederhergestellt werden.

Wenn ein Fehler SemaphoreFullException durch die Release Methode ausgelöst wird, weist sie nicht unbedingt auf ein Problem mit dem aufrufenden Thread hin. Ein Programmierfehler in einem anderen Thread hat möglicherweise dazu geführt, dass der Thread das Semaphor mehrmals verlässt, als er eingegeben wurde.

Wenn das aktuelle Semaphore Objekt ein benanntes System-Semaphor darstellt, muss der Benutzer über Rechte verfügen SemaphoreRights.Modify , und das Semaphor muss mit SemaphoreRights.Modify Rechten geöffnet worden sein.

Weitere Informationen

Gilt für: