|
65 | 65 | <Docs>
|
66 | 66 | <summary>Indicates that the value of a static field is unique for each thread.</summary>
|
67 | 67 | <remarks>
|
68 |
| - <format type="text/markdown"><![CDATA[ |
69 |
| - |
70 |
| -## Remarks |
71 |
| - A `static` field marked with <xref:System.ThreadStaticAttribute> is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value. |
72 |
| - |
73 |
| - Note that in addition to applying the <xref:System.ThreadStaticAttribute> attribute to a field, you must also define it as a `static` field (in C# or F#) or a `Shared` field (in Visual Basic). |
74 |
| - |
| 68 | + <format type="text/markdown"><![CDATA[ |
| 69 | +
|
| 70 | +## Remarks |
| 71 | +
|
| 72 | +A `static` field marked with <xref:System.ThreadStaticAttribute> is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value. |
| 73 | +
|
| 74 | +In addition to applying the <xref:System.ThreadStaticAttribute> attribute to a field, you must also define it as `static` (in C# or F#) or `Shared` (in Visual Basic). |
| 75 | +
|
75 | 76 | > [!NOTE]
|
76 |
| -> Do not specify initial values for fields marked with `ThreadStaticAttribute`, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to `null` if it is a reference type. |
77 |
| - |
78 |
| - Use this attribute as it is, and do not derive from it. |
79 |
| - |
80 |
| - For more information about using attributes, see [Attributes](/dotnet/standard/attributes/). |
81 |
| - |
82 |
| - |
83 |
| - |
84 |
| -## Examples |
85 |
| - The following example instantiates a random number generator, creates ten threads in addition to the main thread, and then generates two million random numbers in each thread. It uses the <xref:System.ThreadStaticAttribute> attribute to calculate the sum and the count of random numbers per thread. It also defines two additional per-thread fields, `previous` and `abnormal`, that allows it to detect corruption of the random number generator. |
86 |
| - |
| 77 | +> Don't specify initial values for fields marked with `ThreadStaticAttribute`. Such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you don't specify an initial value, the field will be initialized to its default value if it's a value type, or to `null` if it's a reference type. |
| 78 | +
|
| 79 | +Use this attribute as it is, and don't derive from it. |
| 80 | +
|
| 81 | +For more information about using attributes, see [Attributes](/dotnet/standard/attributes/). |
| 82 | +
|
| 83 | +## Examples |
| 84 | +
|
| 85 | +The following example code demonstrates how to use the `ThreadStaticAttribute` to ensure that a static field is unique to each thread. In the code, each thread assigns a different request ID to the thread-static field and then simulates request processing across multiple method calls. |
| 86 | +
|
87 | 87 | :::code language="csharp" source="~/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs" id="Snippet1":::
|
88 | 88 | :::code language="fsharp" source="~/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs" id="Snippet1":::
|
89 |
| - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb" id="Snippet1"::: |
90 |
| - |
91 |
| - The example uses the `lock` statement in C#, the `lock` function in F#, and the `SyncLock` construct in Visual Basic to synchronize access to the random number generator. This prevents corruption of the random number generator, which typically results in its returning a value of zero for all subsequent calls. |
92 |
| - |
93 |
| - The example also uses the <xref:System.Threading.CountdownEvent> class to ensure that each thread has finished generating random numbers before it displays the total number of calls. Otherwise, if the main thread completes execution before the additional threads that it spawns, it displays an inaccurate value for the total number of method calls. |
94 |
| - |
| 89 | + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb" id="Snippet1"::: |
| 90 | +
|
95 | 91 | ]]></format>
|
96 | 92 | </remarks>
|
97 | 93 | <altmember cref="T:System.Attribute" />
|
|
0 commit comments