Why there is no Javalike semaphore acquiring multiple permits in C#?

c#, concurrency

Solution

Good api design is a fine art, truly mastered only by a few. One important aspect is to emphasize commonality. The .NET Semaphore class has that, you use `WaitOne()` to acquire the semaphore. Like you do on all of the synchronization classes.

And it becomes especially a fine art by knowing what to leave out. A custom `Aquire(n)` method would be high on that cut list. It is a very troublesome method, the underlying native implementation on Windows doesn't support it either. Which produces the serious risk of inducing deadlock that's next to impossible to debug, the code would be hung inside an invisible method that's looping, buried deep inside the CLR. Which adds lots of semantics to waits that matter a great deal when code needs to be aborted. You're free to loop yourself, a simple workaround and of course well visible when you debug a deadlock, you can at least inspect the counter.

Problem

Why there is no method for acquiring multiple permits on semaphore in C#? I want to use my semaphore like this: ``` semaphore.Acquire(2); ```

Original source