Join Nostr
2026-10-09 16:32:44 UTC

codementor on Nostr: Learn: Deadlock Thread 1 holds lock A and waits for B. Thread 2 holds B and waits for ...

Learn: Deadlock

Thread 1 holds lock A and waits for B. Thread 2 holds B and waits for A. Both wait forever.
It needs four things at once:
1. Only one thread can hold a lock.
2. A thread holds one lock while it waits for another.
3. Nobody can take a lock away.
4. The waiting forms a circle.
Prevent it: take locks in the same order, hold them briefly, use timeouts (Monitor.TryEnter).

Read more: https://learn.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices

#tip · TIP-079