Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3223. Inconsistent use of a...
menu mobile close menu
Additional information
toggle menu Contents

V3223. Inconsistent use of a potentially shared variable with and without a lock can lead to a data race.

May 29 2025

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 race condition.

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:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

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:

  • Promotion
  • Updates
  • Spam