Semaphore.Release 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
退出信号灯。
重载
| 名称 | 说明 |
|---|---|
| Release() |
退出信号灯并返回以前的计数。 |
| Release(Int32) |
退出信号灯的指定次数并返回以前的计数。 |
Release()
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
退出信号灯并返回以前的计数。
public:
int Release();
public int Release();
member this.Release : unit -> int
Public Function Release () As Integer
返回
调用方法之前 Release 信号灯上的计数。
例外
信号灯计数已达到最大值。
命名信号灯出现 Win32 错误。
示例
下面的代码示例创建最大计数为 3 的信号灯,初始计数为零。 该示例启动五个线程,该线程阻止等待信号灯。 主线程使用 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 该方法输入信号灯,它们通常使用此方法重载退出。
如果方法引发 Release aSemaphoreFullException,则不一定指示调用线程出现问题。 另一个线程中的编程错误可能导致该线程退出信号灯的次数超过输入的次数。
如果当前 Semaphore 对象表示命名的系统信号量,则用户必须具有 SemaphoreRights.Modify 权限,并且信号灯必须已使用 SemaphoreRights.Modify 权限打开。
另请参阅
适用于
Release(Int32)
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- Semaphore.cs
- Source:
- 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 错误。
示例
下面的代码示例创建最大计数为 3 的信号灯,初始计数为零。 该示例启动五个线程,该线程阻止等待信号灯。 主线程使用 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
注解
如果线程多次进入信号灯,此方法重载允许通过一次调用还原整个信号灯计数。
如果方法引发 Release aSemaphoreFullException,则不一定指示调用线程出现问题。 另一个线程中的编程错误可能导致该线程退出信号灯的次数超过输入的次数。
如果当前 Semaphore 对象表示命名的系统信号量,则用户必须具有 SemaphoreRights.Modify 权限,并且信号灯必须已使用 SemaphoreRights.Modify 权限打开。