Skip to content


Rust inspired Async Lock for C

Venturing back into C# for the first time in a long time. I'm only 5 minutes in and already felt compelled to create a new utility class.

Essentially I'm adding an ASP.NET Web API to a daemon process, which requires that the REST API can get exclusive access to some daemon in-memory state. Since I'm building everything around async/await, I went looking for the async equivalent of the traditional lock, only to not find one. From what I could gather, the recommendation is to build one around SemaphoreSlim. After using that explicitly, I was really uncomfortable with having to make sure that I a) released the semaphore and b) did it in a try/finally.