This document contains the release information for the .NET Framework ThreadStaticVariables sample. You will find the following sections below:
</p><ul class="Text"><li><a href="#_Sample_Overview">Overview</a></li><li><a href="#_Sample_Topics">Topics</a></li><li><a href="#_Location_of_Sample">Location</a></li><li><a href="#_Building_the_Sample">Building the sample</a></li><li><a href="#_Running_the_Sample">Running the sample</a></li><li><a href="#_sdkvars">Notes</a></li></ul><p class="SectionBreak" /><h4 class="Heading"><a name="_Sample_Overview" />
Overview
</h4><p class="Text">
This sample demonstrates how to use the ThreadStaticAttribute type to create static variables backed by thread local storage. Each thread to access the type will receive its own "thread-local" copy of any static variable marked as [ThreadStatic].
The sample creates two instances of the Thread type and starts each on the same static method. The sample also uses the Join() method to wait for the threads to terminate.
This sample uses the ThreadStaticAttribute to attribute a static member variable. Each thread in the sample then accesses its own copy of the static variable.
This sample is located in the <a href="." title="Jump to Folder" target="_blank">Technologies\Threading\ThreadStaticVariables</a> subdirectory of the .NET Framework SDK samples directory.
The output of this example demonstrates two things. First, it shows that each of the three threads is incrementing its own copy of the static variable. This is evident, because there is no logic in the sample to reset the variable value to zero. Second, it shows that only one thread received a thread-local copy of the variable initialized to a starting value of 10. This is because only one thread calls the static constructor for the type, which is used to initialize static members.