我需要在包含多个线程的应用程序中使用信号量。我的用法可能是一种常见的情况,但是m停留在API上。信号量无一例外地溢出至最大值
在我的用法中,信号量可以从多个点发布,而只有一个线程在信号量上等待。
现在,我需要信号量是一个二进制信号量,也就是说,我需要确保在多个线程同时发送到信号量的情况下,信号计数保持为1,并且不会引发错误。我怎样才能做到这一点。
总之我需要下面的代码才能工作。
private static Semaphore semaphoreResetMapView = new Semaphore(0, 1); // Limiting the max value of semaphore to 1.
void threadWait(){
while (true){
semaphoreResetMapView.WaitOne();
<code>
}
}
void Main(){
tThread = new Thread(threadWait);
tThread.Start();
semaphoreResetMapView.Release(1);
semaphoreResetMapView.Release(1);
semaphoreResetMapView.Release(1); // Multiple Releases should not throw an error. Rather saturate the value of semaphore to 1.
}
我会很感激这方面的帮助。
听起来你应该使用[ManualResetEvent的(http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent .aspx)而不是“Semaphore”。 –
我后来认识到,AutoResetEvent实际上是正确的解决方案... – vishal