Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphore: understanding initial and maximum number of requests

I'm learning c# semaphore and don't understand one point. 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? Does it mean that if I write like this:

var semaphore = new Semaphore(0, 6);

only main thread can use all 6 slots?

like image 779
mtkachenko Avatar asked Nov 08 '25 04:11

mtkachenko


1 Answers

I like 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.

like image 164
Karel Kral Avatar answered Nov 11 '25 01:11

Karel Kral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!