Книга: C# 2008 Programmer

Using Interlocked Class

Using Interlocked Class

Because incrementing and decrementing are such common operations in programming, the .NET Framework class library provides the Interlocked class for performing atomic operations for variables that are shared by multiple threads. You can rewrite the preceding example using the Increment() method from the static Interlocked class:

static void IncrementVar() {
 Interlocked.Increment(ref _value);

You need to pass in the variable to be incremented by reference to the Increment() method. When a thread encounters the Increment() statement, all other threads executing the same statement must wait until the incrementing is done.

The Interlocked class also includes the Decrement() class that, as its name implies, decrements the specified variable by one.

Оглавление книги

Оглавление статьи/книги

Генерация: 1.119. Запросов К БД/Cache: 3 / 0
Вверх Вниз