Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8cf8ed

Browse filesBrowse files
committed
fix example description
1 parent 8f8d032 commit a8cf8ed
Copy full SHA for a8cf8ed

File tree

Expand file treeCollapse file tree

2 files changed

+22
-26
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+22
-26
lines changed

‎snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs

Copy file name to clipboardExpand all lines: snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ static void Main()
2323

2424
static void ProcessRequest(object? requestId)
2525
{
26-
// Assign the request ID to the thread-static field
26+
// Assign the request ID to the thread-static field.
2727
_requestId = requestId as string;
2828

29-
// Simulate request processing across multiple method calls
29+
// Simulate request processing across multiple method calls.
3030
PerformDatabaseOperation();
3131
PerformLogging();
3232
}

‎xml/System/ThreadStaticAttribute.xml

Copy file name to clipboardExpand all lines: xml/System/ThreadStaticAttribute.xml
+20-24Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,29 @@
6565
<Docs>
6666
<summary>Indicates that the value of a static field is unique for each thread.</summary>
6767
<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+
7576
> [!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+
8787
:::code language="csharp" source="~/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs" id="Snippet1":::
8888
:::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+
9591
]]></format>
9692
</remarks>
9793
<altmember cref="T:System.Attribute" />

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.