O'Reilly logo

Microsoft® .NET Compact Framework Kick Start by Ronnie Yates, Erik Rubin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating Variables with the Interlocked Class

The System.Threading.Interlocked class provides means to increment, decrement, and set a variable in a thread-safe manner. When a variable is incremented or decremented with the Interlocked class, the process is atomic. The CPU cannot be taken away while the increment or decrement is in progress.

Why is this important? Consider an application with one thread that can update an integer and many threads that can read it. On the surface this seems like a safe arrangement. However, consider what would happen if a thread updated a variable in a process that took several CPU instructions, and the CPU was taken away before the update finished. At this instant the variable could have an unexpected, often ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required