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

标签 c# multithreading semaphore

正在学习c#信号量,有一点不懂。我可以像这样初始化信号量:

var semaphore = new Semaphore(4, 6);

很多地方都有这样的解释:

If you want to reserve some slots for the calling thread, you can do so by making the first parameter smaller than the second.

这是否意味着只有主线程可以使用剩余的 2 个资源槽?这是否意味着如果我这样写:

var semaphore = new Semaphore(0, 6);

只有主线程可以使用所有 6 个插槽?

最佳答案

我喜欢Albahari's explanation :

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.

关于c# - 信号量:了解初始和最大请求​​数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203040/

相关文章:

c# - P/调用,c# : unsigned char losing a byte

c# - 如何从wcf获取业务异常?

c# - .Net 4.5 无法识别 HttpUtility

java - java中一个线程的输入来自另一个线程的无限等待

c - 选择共享内存或 unix 信号量键的惯用方法

c++ - 信号量未初始化

c# - 禁用 Javascript 时使用 Javascript 提交表单

java - 有没有办法将 ScheduledExecutorService 与 ExecutorCompletionService 一起使用?

windows - 如何在多核系统上完全保留一个核?

c - Sem_post() 无法正常工作,既不会增加信号量的值,也不会解锁附加到该信号量的进程