简体   繁体   English

信号量:了解初始和最大请求​​数

[英]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.

相关问题 Owin中的最大并发请求数 - Maximum number of concurrent requests in Owin 异步/等待最大并发http请求数 - Async/Await maximum number of concurrent http requests 增加WCF应用程序中的最大传出请求数 - Increase the maximum number of outgoing requests in WCF Application 信号量 - 初始计数有什么用? - Semaphore - What is the use of initial count? 如果初始计数为零,为什么信号灯会挂起? - Why does the semaphore hangs if the initial count is zero? 命名信号量名称的最大长度是多少? - What is the maximum length of a named Semaphore's name? 以最大速率处理请求 - Processing requests at a maximum rate 信号量异常 - 将指定的计数添加到信号量会导致它超过其最大计数 - Semaphore exception - Adding the specified count to the semaphore would cause it to exceed its maximum count 将Twitter示例流保存到RavenDB会导致以下异常:已达到该会话允许的最大请求数(30) - Saving a Twitter Sample Stream to RavenDB results in the exception The maximum number of requests (30) allowed for this session has been reached ASP.NET Core WebAPI可以同时处理的最大请求数量是多少? - What is the maximum number of requests that ASP.NET Core WebAPI can handle simultaneously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM