The analyzer has detected inconsistent use of a shared resource: it is used both with and without a lock.
The example:
public class UserSessionManager
{
private static int _activeSessionCount = 0;
private static readonly object _lock = new object();
public void StartSession()
{
lock (_lock)
{
_activeSessionCount++;
....
}
}
public void EndSession()
{
_activeSessionCount--;
....
}
....
}
The UserSessionManager class handles the number of active sessions. The StartSession method correctly increments a counter using lock, but the EndSession method decrements the same counter without locking. This results in inconsistent access synchronization to the shared _activeSessionCount resource.
When multiple threads run concurrently, the lack of locking may lead to a data race.
To ensure correct operation, add lock in the EndSession:
public class UserSessionManager
{
private static int _activeSessionCount = 0;
private static readonly object _lock = new object();
public void StartSession()
{
lock (_lock)
{
_activeSessionCount++;
....
}
}
public void EndSession()
{
lock (_lock)
{
_activeSessionCount--;
....
}
}
....
}
This diagnostic is classified as:
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders:
Take
a chance!