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

The analyzer has detected inconsistent use of a shared resource: it is used both with and without a lock\.

The example:

```cpp
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`:

```cpp
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--;
      ....
    }
  }

  ....
}
```