Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 24
    As this method was introduced into the .NET framework recently, I think we can assume that the concept of locking in an async/await world is now well proven. Commented Sep 12, 2014 at 8:41
  • 11
    For some more info, search for the text "SemaphoreSlim" in this article: Async/Await - Best Practices in Asynchronous Programming Commented Jun 23, 2017 at 20:28
  • 40
    Shouldn't it be initialized as mySemaphoreSlim = new SemaphoreSlim(1, 1) in order to work like lock(...)? Commented May 2, 2018 at 16:14
  • 6
    Added the extended version of this answer: stackoverflow.com/a/50139704/1844247 Commented May 2, 2018 at 16:53
  • 4
    @JamesKo that's the whole point of using a semaphore as a mutex. Only one task should execute Stuff, therefore all other tasks have to wait for the previous task to release lock and take their turn into acquiring it one by one. It's like a bunch of people waiting outside the toilet to use it (having one toilet only). When they say to their friends "i'm going to the toilet" it doesn't mean they go and come back instantly. It means they go, wait for all others to finish, then use the toilet and come back. Commented Jun 22, 2018 at 11:35