Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.27 KB

rule.adoc

File metadata and controls

56 lines (41 loc) · 1.27 KB

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)
  {
    //...
  }
}

See


Implementation Specification

(visible only on this page)


(visible only on this page)