Techniques for making a method non-reentrant

60 views Asked by At

I have an ASP.NET Core application with an async method with a signature something like this.

public async Task RunAsync()
{
    // ...
}

I'm concerned about this method running at the same time by more than one thread.

It looks like I can use lock or a SemaphoreSlim. Can someone help explain these options in a way that I can choose between the two?

Also, does the fact that my method is async impact the decision?

1

There are 1 answers

0
Jonathan Wood On

So, I don't know all the details of how these two methods work, but I found that you cannot use await within a lock. So even though lock appears to have some advantages, in my case, only a SemaphoreSlim will work.