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# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.