All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S6507.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

Locking on a local variable can undermine synchronization because two different threads running the same method in parallel will potentially lock on different instances of the same object, allowing them to access the synchronized block at the same time.

Noncompliant code example

private void DoSomething()
{
  object local = new object();
  // Code potentially modifying the local variable ...

  lock (local) // Noncompliant
  {
    // ...
  }
}

Compliant solution

private readonly object lockObj = new object();

private void DoSomething()
{
  lock (lockObj)
  {
    //...
  }
}

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy