Thread-Specific State

We already know that each thread has its own call stack; hence, stack-allocated data is state-specific to a particular thread (for example, local variables and parameters that are being passed to methods). In the “Synchronization Primitives” section, we discuss how to share state between threads, preferably in a safe manner. For now, let’s focus on other forms of thread-specific state.

The ThreadStatic Attribute

Static state is isolated on a per-application domain basis. Because multiple threads can be executing in the same application domain, such static state is shared across those threads. This behavior can be changed by applying the ThreadStatic attribute to a static field. First, let’s show multiple threads accessing ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.