...

/

Implementing Semaphore

Implementing Semaphore

Learn how to design and implement a simple semaphore class in C#.

Implementing Semaphore

C# does provide its own implementation of Semaphore, however, it is an interesting exercise to learn to implement a semaphore using a monitor.

Briefly, a semaphore is a construct that allows some threads to access a fixed set of resources in parallel. Always think of a semaphore as having a fixed number of permits to give out. Once all the permits are given out, requesting threads, need to wait for a permit to be returned before proceeding forward.

Your task is to implement a semaphore which takes in its constructor the maximum number of permits allowed and is also initialized with the same number of permits. Additionally, if all the permits have been given out, the semaphore blocks threads attempting to acquire it.

widget

Solution

Given the above definition we can now start to think of what functions our Semaphore class will need to expose. We need a function to "gain ...

Access this course and 1400+ top-rated courses and projects.