Skip to content

Commit aee73fc

Browse files
authored
Update docs for CompareExchange\<T> (#11422)
1 parent 651ae50 commit aee73fc

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

xml/System.Threading/Interlocked.xml

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@
6868
6969
The <xref:System.Threading.Interlocked.Increment%2A> and <xref:System.Threading.Interlocked.Decrement%2A> methods increment or decrement a variable and store the resulting value in a single operation. On most computers, incrementing a variable is not an atomic operation, requiring the following steps:
7070
71-
1. Load a value from an instance variable into a register.
72-
73-
2. Increment or decrement the value.
74-
75-
3. Store the value in the instance variable.
71+
1. Load a value from an instance variable into a register.
72+
2. Increment or decrement the value.
73+
3. Store the value in the instance variable.
7674
7775
If you do not use <xref:System.Threading.Interlocked.Increment%2A> and <xref:System.Threading.Interlocked.Decrement%2A>, a thread can be preempted after executing the first two steps. Another thread can then execute all three steps. When the first thread resumes execution, it overwrites the value in the instance variable, and the effect of the increment or decrement performed by the second thread is lost.
7876
@@ -82,7 +80,6 @@
8280
8381
Ensure that any write or read access to a shared variable is atomic. Otherwise, the data might be corrupted or the loaded value might be incorrect.
8482
85-
8683
## Examples
8784
The following code example shows a thread-safe resource locking mechanism.
8885
@@ -611,8 +608,6 @@
611608
## Remarks
612609
If `comparand` and the value in `location1` are equal, then `value` is stored in `location1`. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of <xref:System.Threading.Interlocked.CompareExchange%2A> is the original value in `location1`, whether or not the exchange takes place.
613610
614-
615-
616611
## Examples
617612
The following code example demonstrates a thread-safe method that accumulates a running total of <xref:System.Double> values. Two threads add a series of <xref:System.Double> values using the thread-safe method and ordinary addition, and when the threads complete the totals are compared. On a dual-processor computer, there is a significant difference in the totals.
618613
@@ -729,13 +724,11 @@
729724
## Remarks
730725
If `comparand` and the value in `location1` are equal, then `value` is stored in `location1`. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of <xref:System.Threading.Interlocked.CompareExchange%2A> is the original value in `location1`, whether or not the exchange takes place.
731726
732-
733-
734727
## Examples
735728
The following code example demonstrates a thread-safe method that accumulates a running total. The initial value of the running total is saved, and then the <xref:System.Threading.Interlocked.CompareExchange%2A> method is used to exchange the newly computed total with the old total. If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. In that case, the attempt to update the running total must be repeated.
736729
737730
> [!NOTE]
738-
> The <xref:System.Threading.Interlocked.Add%2A> method, introduced in version 2.0 of the .NET Framework, provides a more convenient way to accumulate thread-safe running totals for integers.
731+
> The <xref:System.Threading.Interlocked.Add%2A> method provides a more convenient way to accumulate thread-safe running totals for integers.
739732
740733
:::code language="csharp" source="~/snippets/csharp/System.Threading/Interlocked/CompareExchange/source2.cs" id="Snippet1":::
741734
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Threading.Interlocked CompareExchange0/VB/source.vb" id="Snippet1":::
@@ -877,7 +870,7 @@
877870
If `comparand` and the value in `location1` are equal, then `value` is stored in `location1`. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of this method is the original value in `location1`, whether or not the exchange takes place.
878871
879872
> [!NOTE]
880-
> <xref:System.IntPtr> is a platform-specific type.
873+
> <xref:System.IntPtr> is a platform-specific type.
881874
882875
]]></format>
883876
</remarks>
@@ -951,12 +944,12 @@
951944
## Remarks
952945
953946
> [!IMPORTANT]
954-
> The <xref:System.Threading.Interlocked.CompareExchange%60%601%28%60%600%40%2C%60%600%2C%60%600%29> method overload provides a type-safe alternative for reference types. We recommend that you call it instead of this overload.
947+
> The <xref:System.Threading.Interlocked.CompareExchange%60%601%28%60%600%40%2C%60%600%2C%60%600%29> method overload provides a generic alternative that can be used for concrete reference types.
955948
956949
If `comparand` and the object in `location1` are equal by reference, then `value` is stored in `location1`. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of <xref:System.Threading.Interlocked.CompareExchange%2A> is the original value in `location1`, whether or not the exchange takes place.
957950
958951
> [!NOTE]
959-
> The objects are compared for reference equality rather than value equality. As a result, two boxed instances of the same value type (for example, the integer 3) always appear to be unequal and no operation is performed. Do not use this overload with value types.
952+
> The objects are compared for reference equality rather than value equality. As a result, two boxed instances of the same value type (for example, the integer 3) always appear to be unequal and no operation is performed. Do not use this overload with value types.
960953
961954
]]></format>
962955
</remarks>
@@ -1341,27 +1334,23 @@ If `comparand` and the object in `location1` are equal by reference, then `value
13411334
<Parameter Name="comparand" Type="T" Index="2" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
13421335
</Parameters>
13431336
<Docs>
1344-
<typeparam name="T">The type to be used for <paramref name="location1" />, <paramref name="value" />, and <paramref name="comparand" />. This type must be a reference type.</typeparam>
1337+
<typeparam name="T">The type to be used for <paramref name="location1" />, <paramref name="value" />, and <paramref name="comparand" />.</typeparam>
13451338
<param name="location1">The destination, whose value is compared by reference with <paramref name="comparand" /> and possibly replaced. This is a reference parameter (<see langword="ref" /> in C#, <see langword="ByRef" /> in Visual Basic).</param>
13461339
<param name="value">The value that replaces the destination value if the comparison by reference results in equality.</param>
13471340
<param name="comparand">The value that is compared by reference to the value at <paramref name="location1" />.</param>
1348-
<summary>Compares two instances of the specified reference type <paramref name="T" /> for reference equality and, if they are equal, replaces the first one, as an atomic operation.</summary>
1341+
<summary>Compares two instances of the specified type <typeparamref name="T" /> for reference equality and, if they're equal, replaces the first one, as an atomic operation.</summary>
13491342
<returns>The original value in <paramref name="location1" />.</returns>
13501343
<remarks>
13511344
<format type="text/markdown"><![CDATA[
13521345
13531346
## Remarks
1354-
If `comparand` and the value in `location1` are equal by reference, then `value` is stored in `location1`. Otherwise, no operation is performed. The comparison and the exchange are performed as an atomic operation. The return value of this method is the original value in `location1`, whether or not the exchange takes place.
13551347
1356-
This method only supports reference types. There are overloads of the <xref:System.Threading.Interlocked.CompareExchange%2A> method for the value types <xref:System.Int32>, <xref:System.Int64>, <xref:System.IntPtr>, <xref:System.Single>, and <xref:System.Double>, but there is no support for other value types.
1357-
1358-
> [!NOTE]
1359-
> This method overload is preferable to the <xref:System.Threading.Interlocked.CompareExchange%28System.Object%40%2CSystem.Object%2CSystem.Object%29> method overload, because the latter requires the destination object to be accessed late-bound.
1348+
If `comparand` and the value in `location1` are equal by reference, then `value` is stored in `location1`. Otherwise, no operation is performed. The comparison and the exchange are performed as an atomic operation. The return value of this method is the original value in `location1`, whether or not the exchange takes place.
13601349
13611350
]]></format>
13621351
</remarks>
13631352
<exception cref="T:System.NullReferenceException">The address of <paramref name="location1" /> is a null pointer.</exception>
1364-
<exception cref="T:System.NotSupportedException">An unsupported <typeparamref name="T" /> is specified.</exception>
1353+
<exception cref="T:System.NotSupportedException">An unsupported <typeparamref name="T" /> is specified. On .NET 8 and earlier versions, <typeparamref name="T" /> must be a reference type. On .NET 9 and later versions, <typeparamref name="T" /> must be a reference, primitive, or enum type.</exception>
13651354
</Docs>
13661355
</Member>
13671356
<MemberGroup MemberName="Decrement">
@@ -2008,7 +1997,7 @@ If `comparand` and the object in `location1` are equal by reference, then `value
20081997
## Remarks
20091998
20101999
> [!IMPORTANT]
2011-
> The <xref:System.Threading.Interlocked.Exchange%60%601%28%60%600%40%2C%60%600%29> method overload provides a type-safe alternative for reference types. We recommend that you call it instead of this overload.
2000+
> The <xref:System.Threading.Interlocked.Exchange%60%601%28%60%600%40%2C%60%600%29> method overload provides a generic alternative that can be used for concrete reference types.
20122001
20132002
]]></format>
20142003
</remarks>
@@ -2371,23 +2360,14 @@ If `comparand` and the object in `location1` are equal by reference, then `value
23712360
<Parameter Name="value" Type="T" Index="1" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
23722361
</Parameters>
23732362
<Docs>
2374-
<typeparam name="T">The type to be used for <paramref name="location1" /> and <paramref name="value" />. This type must be a reference type.</typeparam>
2363+
<typeparam name="T">The type to be used for <paramref name="location1" /> and <paramref name="value" />.</typeparam>
23752364
<param name="location1">The variable to set to the specified value. This is a reference parameter (<see langword="ref" /> in C#, <see langword="ByRef" /> in Visual Basic).</param>
23762365
<param name="value">The value to which the <paramref name="location1" /> parameter is set.</param>
2377-
<summary>Sets a variable of the specified type <paramref name="T" /> to a specified value and returns the original value, as an atomic operation.</summary>
2366+
<summary>Sets a variable of the specified type <typeparamref name="T" /> to a specified value and returns the original value, as an atomic operation.</summary>
23782367
<returns>The original value of <paramref name="location1" />.</returns>
2379-
<remarks>
2380-
<format type="text/markdown"><![CDATA[
2381-
2382-
## Remarks
2383-
This method only supports reference types. There are overloads of the <xref:System.Threading.Interlocked.Exchange%2A> method for the <xref:System.Int32>, <xref:System.Int64>, <xref:System.IntPtr>, <xref:System.Single>, and <xref:System.Double> value types, but there is no support for other value types.
2384-
2385-
> [!NOTE]
2386-
> This method overload is preferable to the <xref:System.Threading.Interlocked.Exchange%28System.Object%40%2CSystem.Object%29> method overload, because the latter requires late-bound access to the destination object .
2387-
]]></format>
2388-
</remarks>
2368+
<remarks>To be added.</remarks>
23892369
<exception cref="T:System.NullReferenceException">The address of <paramref name="location1" /> is a <see langword="null" /> pointer.</exception>
2390-
<exception cref="T:System.NotSupportedException">An unsupported <typeparamref name="T" /> is specified.</exception>
2370+
<exception cref="T:System.NotSupportedException">An unsupported <typeparamref name="T" /> is specified. On .NET 8 and earlier versions, <typeparamref name="T" /> must be a reference type. On .NET 9 and later versions, <typeparamref name="T" /> must be a reference, primitive, or enum type.</exception>
23912371
</Docs>
23922372
</Member>
23932373
<MemberGroup MemberName="Increment">

0 commit comments

Comments
 (0)