Code inspection: Suspicious locking over synchronization primitive
This inspection reports lock statements that use a synchronization primitive, such as SemaphoreSlim, ManualResetEvent, or ReaderWriterLockSlim, as the lock object. Locking on these objects is suspicious because they already have their own synchronization semantics. Mixing lock with them can be confusing and can hide threading bugs.
Example
using System.Threading;
class C
{
private readonly SemaphoreSlim myGate = new(1, 1);
void M()
{
lock (myGate)
{
DoWork();
}
}
}
using System.Threading;
class C
{
private readonly object myLock = new();
void M()
{
lock (myLock)
{
DoWork();
}
}
}
01 April 2026