This is dangerous:
lock(this)
{
// critical section
}
❌ Why it fails
-
External code can also lock on
this -
Deadlocks appear randomly
✅ Correct Pattern
private readonly object _sync = new();
lock (_sync)
{
// safe
}
Daily micro-tips for C#, SQL, performance, and scalable backend engineering.
This is dangerous:
lock(this)
{
// critical section
}
External code can also lock on this
Deadlocks appear randomly
private readonly object _sync = new();
lock (_sync)
{
// safe
}