Using Interlocked.CompareExchange to increment a counter until a value
问题 I need to increment a counter until it reaches a particular number. I can use two parallel task to increment the number. Instead of using a lock to check if the number has not reach the maximum allowed value and then incrementing, I thought using Interlocked.CompareExchange in the following manner: public class CompareExchangeStrategy { private int _counter = 0; private int _max; public CompareExchangeStrategy(int max) { _max = max; } public void Increment() { Task task1 = new Task(new Action