Skip to content

Gendarme.Rules.Concurrency.ReviewLockUsedOnlyForOperationsOnVariablesRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

ReviewLockUsedOnlyForOperationsOnVariablesRule

Assembly: Gendarme.Rules.Concurrency
Version: git

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);

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally