As a question of a job interview, what is the best&simple way to implement a semaphore using C#?
Please add your code
Thanks!
* Not using semphores with Win32 API, I mean To implement it yourself.
As a question of a job interview, what is the best&simple way to implement
Share
The .NET framework comes with a Semaphore, so that’d be the easiest way to make one:
But I’m guessing the interviewer is more interested in the structure behind it…
You would simply need a globally hosted, access controlled entity that external entities can call into to get and release locks. In C# you could do something similar by hosting a web service or remoting class that exposes such methods and can keep a count of active locks.
When the lock limit is reached, you simply return a negative response, such as false to signify that the lock cannot be attained.
Adapted from Parv Sharma in the comments. You might be looking at something like this. The semaphore needs to lock access to
resources(as acquiring a semaphore slot is also a guarded action), but theresourcesthemselves are what control the number of locks available. The application logic would then be dictating what exactly is being locked.