Semaphore.Release Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Quitte le sémaphore.
Surcharges
| Nom | Description |
|---|---|
| Release() |
Quitte le sémaphore et retourne le nombre précédent. |
| Release(Int32) |
Quitte le sémaphore un nombre spécifié de fois et retourne le nombre précédent. |
Release()
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
Quitte le sémaphore et retourne le nombre précédent.
public:
int Release();
public int Release();
member this.Release : unit -> int
Public Function Release () As Integer
Retours
Le nombre sur le sémaphore avant l’appel de la Release méthode.
Exceptions
Le nombre de sémaphores est déjà à la valeur maximale.
Une erreur Win32 s’est produite avec un sémaphore nommé.
Le sémaphore actuel représente un sémaphore système nommé, mais l’utilisateur n’a Modifypas .
- ou -
Le sémaphore actuel représente un sémaphore système nommé, mais il n’a pas été ouvert avec Modify.
Exemples
L’exemple de code suivant crée un sémaphore avec un nombre maximal de trois et un nombre initial de zéro. L’exemple démarre cinq threads, ce qui bloque l’attente du sémaphore. Le thread principal utilise la Release(Int32) surcharge de méthode pour augmenter le nombre de sémaphores au maximum, ce qui permet à trois threads d’entrer dans le sémaphore. Chaque thread utilise la méthode pour attendre une seconde, pour simuler le Thread.Sleep travail, puis appelle la Release() surcharge de méthode pour libérer le sémaphore.
Chaque fois que le sémaphore est libéré, le nombre de sémaphores précédents s’affiche. Les messages de console suivent l’utilisation du sémaphore. L’intervalle de travail simulé est légèrement augmenté pour chaque thread, afin de faciliter la lecture de la sortie.
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
Remarques
Les threads utilisent généralement la WaitOne méthode pour entrer le sémaphore, et ils utilisent généralement cette surcharge de méthode pour quitter.
Si une SemaphoreFullException exception est levée par la Release méthode, elle n’indique pas nécessairement un problème avec le thread appelant. Une erreur de programmation dans un autre thread peut avoir provoqué la sortie du sémaphore plus de fois que celui entré.
Si l’objet actuel Semaphore représente un sémaphore système nommé, l’utilisateur doit avoir SemaphoreRights.Modify des droits et le sémaphore doit avoir été ouvert avec SemaphoreRights.Modify des droits.
Voir aussi
S’applique à
Release(Int32)
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
Quitte le sémaphore un nombre spécifié de fois et retourne le nombre précédent.
public:
int Release(int releaseCount);
public int Release(int releaseCount);
member this.Release : int -> int
Public Function Release (releaseCount As Integer) As Integer
Paramètres
- releaseCount
- Int32
Nombre de fois pour quitter le sémaphore.
Retours
Le nombre sur le sémaphore avant l’appel de la Release méthode.
Exceptions
releaseCount est inférieur à 1.
Le nombre de sémaphores est déjà à la valeur maximale.
Une erreur Win32 s’est produite avec un sémaphore nommé.
Le sémaphore actuel représente un sémaphore système nommé, mais l’utilisateur n’a Modify pas de droits.
- ou -
Le sémaphore actuel représente un sémaphore système nommé, mais il n’a pas été ouvert avec Modify des droits.
Exemples
L’exemple de code suivant crée un sémaphore avec un nombre maximal de trois et un nombre initial de zéro. L’exemple démarre cinq threads, ce qui bloque l’attente du sémaphore. Le thread principal utilise la Release(Int32) surcharge de méthode pour augmenter le nombre de sémaphores au maximum, ce qui permet à trois threads d’entrer dans le sémaphore. Chaque thread utilise la méthode pour attendre une seconde, pour simuler le Thread.Sleep travail, puis appelle la Release() surcharge de méthode pour libérer le sémaphore.
Chaque fois que le sémaphore est libéré, le nombre de sémaphores précédents s’affiche. Les messages de console suivent l’utilisation du sémaphore. L’intervalle de travail simulé est légèrement augmenté pour chaque thread, afin de faciliter la lecture de la sortie.
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
Remarques
Si un thread a entré le sémaphore plusieurs fois, cette surcharge de méthode permet de restaurer le nombre de sémaphores entiers avec un seul appel.
Si une SemaphoreFullException exception est levée par la Release méthode, elle n’indique pas nécessairement un problème avec le thread appelant. Une erreur de programmation dans un autre thread peut avoir provoqué la sortie du sémaphore plus de fois que celui entré.
Si l’objet actuel Semaphore représente un sémaphore système nommé, l’utilisateur doit avoir SemaphoreRights.Modify des droits et le sémaphore doit avoir été ouvert avec SemaphoreRights.Modify des droits.