我正在使用 C# 处理信号量。以下是我对C#中Release
和WaitOne
方法的理解。
WaitOne
方法会在线程进入槽时减少信号量计数,而当线程离开槽时,信号量会增加。
Release
方法返回之前的信号量计数对吗?我的理解与以下代码相矛盾:
static Thread[] threads = new Thread[5];
static Semaphore sem = new Semaphore(3,5);
static void SemaphoreTest()
{
Console.WriteLine("{0} is waiting in line...", Thread.CurrentThread.Name);
Console.WriteLine("Semaphore count : "+sem.WaitOne());
Console.WriteLine("{0} enters the semaphore test", Thread.CurrentThread.Name);
Thread.Sleep(300);
Console.WriteLine("{0} is leaving the semaphore test and the semaphore count is {1}", Thread.CurrentThread.Name, sem.Release());
}
static void Main(string[] args)![enter image description here][2]
{
for (int i = 0; i < 5; i++)
{
threads[i] = new Thread(SemaphoreTest);
threads[i].Name = "thread_" + i;
threads[i].Start();
}
Console.Read();
Thread_2 离开,因此必须增加信号量计数。但这并没有发生,因为当 thread_0 即将离开时,先前的信号量计数为 0。根据我的理解,它必须是一个。我对吗?谁能解释一下?
最佳答案
文档是这样描述Release()
方法的返回值的:
The count on the semaphore before the Release method was called
请注意,这不同于说该方法返回信号量在释放之前的值。
特别是,如果您有两个线程竞相释放信号量,它们可以有效地同时调用 Release()
,导致两个线程的此值相同。也就是说,调用方法与实际释放信号量并不完全相同;该方法必须在信号量实际释放之前做一些工作,并且信号量的计数在实际发生之前保持不变。
我还要指出:至少在我运行测试时,我看到线程 #3 在线程 #0 报告离开之前获取了信号量。现在,我们不确定这些操作实际发生的顺序是什么(控制台输出实际上可能与线程与信号量交互的顺序完全相同,也可能不完全相同),但另一种可能性是线程 #3 获得了信号量在线程 #2 释放它之后,但在线程 #0 释放它之前。
在许多有效场景中,您会看到连续返回计数 0。
关于c# - C# 信号量中的 Release 和 WaitOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020161/