[英]Semaphore: understanding initial and maximum number of requests
I'm learning c# semaphore and don't understand one point.我正在学习 c# semaphore 并且不明白一点。 I can initialize Semaphore like this:我可以像这样初始化信号量:
var semaphore = new Semaphore(4, 6);
There is such explanation in many places:很多地方都有这样的解释:
If you want to reserve some slots for the calling thread, you can do so by making the first parameter smaller than the second.如果要为调用线程保留一些插槽,可以通过使第一个参数小于第二个参数来实现。
Does it mean that only main thread can use remaining 2 resource slots?这是否意味着只有主线程可以使用剩余的 2 个资源槽? Does it mean that if I write like this:这是否意味着如果我这样写:
var semaphore = new Semaphore(0, 6);
only main thread can use all 6 slots?只有主线程可以使用所有 6 个插槽?
I like Albahari's explanation :我喜欢Albahari 的解释:
A semaphore is like a nightclub: it has a certain capacity, enforced by a bouncer.信号量就像夜总会:它有一定的容量,由保镖强制执行。 Once it's full, no more people can enter, and a queue builds up outside.一旦满了,就没有更多的人可以进入,外面就排起了长队。 Then, for each person that leaves, one person enters from the head of the queue.然后,对于每个离开的人,一个人从队列的头部进入。 The constructor requires a minimum of two arguments: the number of places currently available in the nightclub and the club's total capacity.构造函数至少需要两个参数:夜总会当前可用的位置数量和俱乐部的总容量。
Unlike lock (Monitor) and Mutex, Semaphore has no “owner” — it's thread-agnostic.与锁(监视器)和互斥锁不同,信号量没有“所有者” ——它与线程无关。 Any thread can call Release on a Semaphore, whereas with Mutex and lock, only the thread that obtained the lock can release it.任何线程都可以在 Semaphore 上调用 Release,而对于 Mutex 和 lock,只有获得锁的线程才能释放它。
Initial value can be used to initiate number of requests for the semaphore that can be granted concurrently.初始值可用于发起可并发授予的信号量请求数。 it sets your currently available concurrency level for related sempahore.它为相关信号量设置当前可用的并发级别。
While maximum count sets maximum number of requests for the semaphore that can be granted concurrently.而最大计数设置可以同时授予的信号量的最大请求数。 it sets your maximum potential concurrency for related semaphore.它为相关信号量设置了最大的潜在并发性。
You can't increment the counter CurrentCount
greater than maximum count which you set in initialization.您不能将计数器CurrentCount
大于您在初始化中设置的最大计数。
Following sample shows how Semaphores are Thread agnostic:以下示例显示了信号量如何与线程无关:
private static Semaphore semaphore = new Semaphore(3, 6);
private static void Main(string[] args)
{
//semaphore.Release(); //openning another slot for concurreny
semaphore.WaitOne();
Console.WriteLine("main0");
new Thread(() =>
{
semaphore.WaitOne();
Console.WriteLine("thread0");
semaphore.WaitOne();
Console.WriteLine("thread1");
Thread.Sleep(3000);
Console.WriteLine("uncomment the release line to make main1 get in");
}).Start();
Thread.Sleep(1000);
semaphore.WaitOne();
Console.WriteLine("main1");
Console.ReadKey();
}
for more information have a look at http://www.albahari.com/threading/part2.aspx#_Semaphore有关更多信息,请查看http://www.albahari.com/threading/part2.aspx#_Semaphore
Unlike a mutex, a semaphore can Release() and WaitOne() in any thread.与互斥体不同,信号量可以在任何线程中使用 Release() 和 WaitOne()。 WaitOne() — counter in Semaphore decrement. WaitOne() — 信号量递减的计数器。 If counter == 0, blocks until it increases.如果 counter == 0,则阻塞直到它增加。 Release() — counter in Semaphore increment. Release() — 以信号量递增的计数器。 Second constructor parameter == counter, blocks until it decreases.第二个构造函数参数 == 计数器,阻塞直到它减少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.