|
68 | 68 |
|
69 | 69 | 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:
|
70 | 70 |
|
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. |
76 | 74 |
|
77 | 75 | 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.
|
78 | 76 |
|
|
82 | 80 |
|
83 | 81 | 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.
|
84 | 82 |
|
85 |
| -
|
86 | 83 | ## Examples
|
87 | 84 | The following code example shows a thread-safe resource locking mechanism.
|
88 | 85 |
|
|
611 | 608 | ## Remarks
|
612 | 609 | 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.
|
613 | 610 |
|
614 |
| -
|
615 |
| -
|
616 | 611 | ## Examples
|
617 | 612 | 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.
|
618 | 613 |
|
|
729 | 724 | ## Remarks
|
730 | 725 | 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.
|
731 | 726 |
|
732 |
| -
|
733 |
| -
|
734 | 727 | ## Examples
|
735 | 728 | 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.
|
736 | 729 |
|
737 | 730 | > [!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. |
739 | 732 |
|
740 | 733 | :::code language="csharp" source="~/snippets/csharp/System.Threading/Interlocked/CompareExchange/source2.cs" id="Snippet1":::
|
741 | 734 | :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Threading.Interlocked CompareExchange0/VB/source.vb" id="Snippet1":::
|
|
877 | 870 | 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.
|
878 | 871 |
|
879 | 872 | > [!NOTE]
|
880 |
| -> <xref:System.IntPtr> is a platform-specific type. |
| 873 | +> <xref:System.IntPtr> is a platform-specific type. |
881 | 874 |
|
882 | 875 | ]]></format>
|
883 | 876 | </remarks>
|
|
951 | 944 | ## Remarks
|
952 | 945 |
|
953 | 946 | > [!IMPORTANT]
|
954 |
| -> Beginning with .NET Framework 2.0, 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 type-safe alternative. We recommend that you call it instead of this overload. |
955 | 948 |
|
956 | 949 | 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.
|
957 | 950 |
|
958 | 951 | > [!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. |
960 | 953 |
|
961 | 954 | ]]></format>
|
962 | 955 | </remarks>
|
@@ -1354,13 +1347,10 @@ If `comparand` and the object in `location1` are equal by reference, then `value
|
1354 | 1347 |
|
1355 | 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.
|
1356 | 1349 |
|
1357 |
| -> [!NOTE] |
1358 |
| -> 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. |
1359 |
| -
|
1360 | 1350 | ]]></format>
|
1361 | 1351 | </remarks>
|
1362 | 1352 | <exception cref="T:System.NullReferenceException">The address of <paramref name="location1" /> is a null pointer.</exception>
|
1363 |
| - <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> |
1364 | 1354 | </Docs>
|
1365 | 1355 | </Member>
|
1366 | 1356 | <MemberGroup MemberName="Decrement">
|
@@ -2007,7 +1997,7 @@ If `comparand` and the value in `location1` are equal by reference, then `value`
|
2007 | 1997 | ## Remarks
|
2008 | 1998 |
|
2009 | 1999 | > [!IMPORTANT]
|
2010 |
| -> Beginning with .NET Framework 2.0, 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 type-safe alternative. We recommend that you call it instead of this overload. |
2011 | 2001 |
|
2012 | 2002 | ]]></format>
|
2013 | 2003 | </remarks>
|
@@ -2375,13 +2365,9 @@ If `comparand` and the value in `location1` are equal by reference, then `value`
|
2375 | 2365 | <param name="value">The value to which the <paramref name="location1" /> parameter is set.</param>
|
2376 | 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>
|
2377 | 2367 | <returns>The original value of <paramref name="location1" />.</returns>
|
2378 |
| - <remarks> |
2379 |
| - <format type="text/markdown"><![CDATA[ |
2380 |
| -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. |
2381 |
| - ]]></format> |
2382 |
| - </remarks> |
| 2368 | + <remarks>To be added.</remarks> |
2383 | 2369 | <exception cref="T:System.NullReferenceException">The address of <paramref name="location1" /> is a <see langword="null" /> pointer.</exception>
|
2384 |
| - <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> |
2385 | 2371 | </Docs>
|
2386 | 2372 | </Member>
|
2387 | 2373 | <MemberGroup MemberName="Increment">
|
|
0 commit comments