Skip to content

Commit 8756dd1

Browse files
committed
Improve documentation for InterlockedOps
Revised descriptions of the `InterlockedOps` class and its methods (`Xor`, `ClearBits`, `SetBits`) to enhance readability and highlight differences in return values. Clarified the section on conditional update operations, emphasizing predicate delegates and race condition handling. Mentioned additional overloads for condition operations to avoid closures.
1 parent ce0f6e0 commit 8756dd1

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Source/Docs/articles/interlockedops.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Interlocked Operations
22

3-
The `Interlocked` class provides atomic operations for variables that are shared between threads. These operations are guaranteed to be atomic, which means that they are thread-safe and can be used in a multithreaded environment without the need for locks.
3+
The `Interlocked` class provides atomic operations for variables that are shared between threads. These operations are guaranteed atomic, meaning they are thread-safe and can be used in a multithreaded environment without locking.
44

55
The [`InterlockedOps`](xref:KZDev.PerfUtils.InterlockedOps) class provides a set of static helper methods that provide additional functionality that is not currently available in the `Interlocked` class.
66

7-
All of the operations provided by the `InterlockedOps` class are convenience methods that can be implemented directly in code using the existing `Interlocked` class. However, the `InterlockedOps` class provides a more readable, concise, and consistent way to perform the provided operations than repeatedly copying and pasting code. This can make the code easier to understand and maintain by reducing code duplication.
7+
All the operations provided by the `InterlockedOps` class are convenience methods that can be implemented directly in code using the existing `Interlocked` class. However, the `InterlockedOps` class offers a more readable, concise, and consistent way to perform the provided operations than repeatedly copying and pasting code. Reducing code duplication can make the code easier to understand and maintain.
88

99
## Variable Exclusive OR Operations
1010

11-
The `InterlockedOps` class provides the [`Xor`](xref:KZDev.PerfUtils.InterlockedOps.Xor*) method, which performs an atomic XOR operation on a variable, storing the operation result in the provided variable and returning the original value of the variable. This method is useful for toggling an integer variable bit value between `1` and `0` in a thread-safe manner.
11+
The `InterlockedOps` class provides the [`Xor`](xref:KZDev.PerfUtils.InterlockedOps.Xor*) method, which performs an atomic XOR operation on a variable, stores the operation result in the provided variable, and returns the variable's original value. This method is useful for toggling an integer variable bit value between `1` and `0` in a thread-safe manner.
1212

1313
The operation can be performed on any integer type, including `int`, `long`, `uint`, and `ulong`.
1414

@@ -27,21 +27,21 @@ public class XorExample
2727

2828
## Variable ClearBits Operations
2929

30-
The [`ClearBits`](xref:KZDev.PerfUtils.InterlockedOps.ClearBits*) methods are used to clear (set to 0) one or more bits that are currently set (set to 1) in a thread-safe atomic operation. The methods return a value tuple of the original value and new value of the variable at the instant the operation was applied.
30+
The [`ClearBits`](xref:KZDev.PerfUtils.InterlockedOps.ClearBits*) methods are used to clear (set to 0) one or more bits that are currently set (set to 1) in a thread-safe atomic operation. The methods return a value tuple of the original value and a new value of the variable at the instant the operation was applied.
3131

3232
## Variable SetBits Operations
3333

34-
The [`SetBits`](xref:KZDev.PerfUtils.InterlockedOps.SetBits*) methods are simply semantically clearer equivalents of the `Interlocked` [`Or`](xref:System.Threading.Interlocked.Or*). The one difference is that the `SetBits` method returns a value tuple of the original value and new value of the variable at the instant the operation was applied.
34+
The [`SetBits`](xref:KZDev.PerfUtils.InterlockedOps.SetBits*) methods are semantically clearer equivalents of the `Interlocked` [`Or`](xref:System.Threading.Interlocked.Or*). The difference is that the SetBits method returns a value tuple of the original value and a new variable value when the operation is applied.
3535

3636
## Conditional Update Operations
3737

38-
The `InterlockedOps` class provides conditional bitwise update operations for [`And`](xref:KZDev.PerfUtils.InterlockedOps.ConditionAnd*), [`Or`](xref:KZDev.PerfUtils.InterlockedOps.ConditionOr*), [`Xor`](xref:KZDev.PerfUtils.InterlockedOps.ConditionXor*), [`ClearBits`](xref:KZDev.PerfUtils.InterlockedOps.ConditionClearBits*), and [`SetBits`](xref:KZDev.PerfUtils.InterlockedOps.ConditionSetBits*) that allow you to update a variable based on a boolean condition. These methods are useful for implementing lock-free algorithms that require atomic updates to a variable that depend on a dynamic condition and guaranteeing that the operation only occurs if the condition is met in a thread-safe manner.
38+
The `InterlockedOps` class provides conditional bitwise update operations for [`And`](xref:KZDev.PerfUtils.InterlockedOps.ConditionAnd*), [`Or`](xref:KZDev.PerfUtils.InterlockedOps.ConditionOr*), [`Xor`](xref:KZDev.PerfUtils.InterlockedOps.ConditionXor*), [`ClearBits`](xref:KZDev.PerfUtils.InterlockedOps.ConditionClearBits*), and [`SetBits`](xref:KZDev.PerfUtils.InterlockedOps.ConditionSetBits*), allowing you to update a variable based on a boolean condition. These methods help implement lock-free algorithms that require atomic updates to a variable that depends on a dynamic condition and guarantee that the operation only occurs if the condition is met in a thread-safe manner.
3939

40-
The conditional methods are the same as the non-conditional methods except these methods also take a predicate delegate that gets called with the current value of the variable and the predicate determines of the corresponding operation should be applied based on the value of the variable at that instant. If there is a race condition and the value of the variable is changed by another thread, the predicate delegate will be called again with the new variable value and continue until the newly computed value can be applied, or the predicate returns `false`.
40+
The conditional methods are the same as the non-conditional methods, except these methods also take a predicate delegate that gets called with the current value of the variable. The predicate determines whether the corresponding operation should be applied based on the variable's value at that instant. When there is a race condition, and another thread changes the variable's value between the start of the condition delegate call, and applying the operation to the variable, the predicate delegate will be called again with the new variable value and continue until the newly computed value can be applied or the predicate returns `false`.
4141

42-
The conditional methods all return a value type that has the original variable value and the new value at the instant the operation was performed. If the condition predicate returns false, both of the value tuple values will be the same and set to the value of the variable that was passed to the condition predicate that returned false.
42+
The conditional methods all return a value tuple with the original variable value and the new value at the instant the operation was performed. If the condition predicate returns false, both value tuple values will be the same and set to the value of the variable passed to the condition predicate that returned false.
4343

44-
There are also overloads of the condition operations that take a predicate argument to avoid closures when the predicate needs additional values to process the conditional logic.
44+
Also, overloads of the condition operations take a predicate-argument to avoid closures when the predicate needs additional values to process the conditional logic.
4545

4646
```csharp
4747
public class ConditionXorExample

0 commit comments

Comments
 (0)