Skip to content

Gendarme.Rules.Concurrency.ReviewLockUsedOnlyForOperationsOnVariablesRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

ReviewLockUsedOnlyForOperationsOnVariablesRule

Assembly: Gendarme.Rules.Concurrency
Version: 2.10

Description

This rule checks if a lock is used only to perform operations on locals or fields. If the only purpose of that critical section is to make sure the variables are modified atomatically then the methods provided by System.Threading.Interlocked class will be more efficient.

Examples

Bad example:

lock (_lockObject) {
    _counter++;
}

Good example:

Interlocked.Increment(_counter);

Bad example:

lock (_lockObject) {
    _someSharedObject = anotherObject;
}

Good example:

Interlocked.Exchange(_someSharedObject, anotherObject);

Clone this wiki locally