You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xml/System/ThreadStaticAttribute.xml
+3-2Lines changed: 3 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,7 @@
63
63
## Remarks
64
64
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.
65
65
66
-
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 a `Shared` field (in Visual Basic).
66
+
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).
67
67
68
68
> [!NOTE]
69
69
> 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.
@@ -78,9 +78,10 @@
78
78
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.
The example uses the `lock` statement in C# 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.
84
+
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.
84
85
85
86
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.
0 commit comments