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
> `ShouldExitCurrentIteration` would be true after `Stop()` or `Break()` or any exception was thrown.
118
+
> `state.ShouldExitCurrentIteration` would be true after `state.Stop()` or `state.Break()` or any exception was thrown.
121
119
122
120
> [!TIP]
123
-
> Additionally you could use `IsStopped` and `IsExceptional` to coordinate in other running iterations when `Stop()` was called or any exception was thrown from any iteration.
121
+
> Additionally you could use `state.IsStopped` and `state.IsExceptional` to coordinate in other running iterations when `state.Stop()` was called or any exception was thrown from any iteration.
124
122
125
123
## Exception Handling
126
124
@@ -134,11 +132,11 @@ try {
134
132
Console.WriteLine(n);
135
133
136
134
if (int.IsOddInteger(n))
137
-
thrownewException(); // multiple thread would throw this
135
+
thrownewException(); // multiple threads would throw this // [!code highlight]
138
136
});
139
137
} catch (AggregateExceptionex) {
140
138
ex.Handle(iex=> {
141
-
Console.WriteLine(iex.Message); // write this for multiple times for thrown from multiple threads
139
+
Console.WriteLine(iex.Message); // write this for multiple times for thrown from multiple threads // [!code highlight]
// would hit here since cancellation should be succeeded // [!code highlight]
190
188
Console.WriteLine($"{nameof(OperationCanceledException)} was propagated directly"); // [!code highlight]
191
-
}// [!code highlight]
189
+
}
192
190
```
193
191
194
192
## Performance Enhancement
195
193
196
194
### Thread-Local Storage
197
195
198
-
If one could calculate partially on **each worker thread**(the thread manages a batch of iterations), and finally add up all partial results to the target variable, it could be much more efficient than contenting one single variable from threads.
199
-
Such approach is call**Thread-Local Storage**, a dedicated storage target for each worker thread.
196
+
If one could calculate partially on **each worker thread**(the thread manages a batch of iterations), and finally add up all partial results to the target variable, it could be much more efficient than contenting one single variable from each iteration.
197
+
Such approach is called**Thread-Local Storage**, a dedicated storage target for each worker thread.
200
198
The design is pretty similar to `Enumerable.Aggregate` that folds calculation base on a given initial value on each iteration.
201
199
202
200
```cs
@@ -221,7 +219,7 @@ Console.WriteLine(size);
221
219
### Partitioning
222
220
223
221
Partitioning is a trade-off solution when **invoking callback delegates in parallel loop is way too expensive** and **the operation within the delegate body is relatively fast enough**.
224
-
So one can partition items from source with specified count into **ranges** and process each range **within a same thread**(because each operation is fast enough), so this reduces the cost of involing delegate callback by reducing the thread count started by the loop.
222
+
So one can partition items from source with specified count into **ranges** and process each range **within a same thread**(because each operation is fast enough), so this reduces the cost of invoking delegate callback by reducing the thread count started by the loop.
225
223
226
224
> [!NOTE]
227
225
>`Partitioner` requires collections **with indexer** to work with, it's the only way to represent a range.
@@ -230,7 +228,7 @@ So one can partition items from source with specified count into **ranges** and
230
228
// calculating sum of a large array is a good example for partitioning
231
229
// for it has simple operation on adding up
232
230
// and to avoid callback on each iteration
233
-
// optionally you could avoid resource contention by Thread-Local storage
231
+
// optionally you could reduce resource contention by Thread-Local storage
0 commit comments