Lock keyword Lock type and thread starvation #107232
-
Does |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
They do: https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#threading but this is still a mitigation - it is recommended, as discussed in the blog post, to use appropriate asynchronous constructs: SemaphoreSlim and WaitAsync, TaskCompletionSource, etc. |
Beta Was this translation helpful? Give feedback.
-
I think I asked the question in a wrong way. Let's take 3 threads (a,b,c). All of them access a shared resource in mutual exclusion using the |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
You're not even guaranteed that
b
andc
will swap between them,c
might release the resource and immediately "win" the lock race. The underlying OS mechanisms are not guaranteed to be fair. They are roughly FIFO, so threads will eventually make progress, but if that isn't sufficient you'd need to try other mitigations.Generally speaking, you need to design your application in such a way that this isn't an issue. What, specifically, that needs to be depends entirely on what the resource is and what modifications you're trying to do.