방금 SemaphoreSlim
을 배우기 시작했는데,이 프로그램에서 semaphore.CurrentCount가 어떻게 증가하고 감소합니까? 내 이해에 따라 semaphore.Wait()
을 호출하면 릴리스 카운터가 1 씩 감소하고 semaphore.Release()
일 때 두 스레드가 실행될 수 있지만 semaphore.CurrentCount
은 어떻게 증가합니까? 0 또는 1에서 시작합니까?이 컨텍스트에서 semaphore.CurrentCount는 어떻게 작동합니까?
var semaphore = new SemaphoreSlim(2, 10);
for (int i = 0; i < 20; i++)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("Entering task " + Task.CurrentId);
semaphore.Wait(); //releasecount--
Console.WriteLine("Processing task " + Task.CurrentId);
});
}
while (semaphore.CurrentCount <= 2)
{
Console.WriteLine("Semaphore count: " + semaphore.CurrentCount);
Console.ReadKey();
semaphore.Release(2);
}
Console.ReadKey();