2

Where are the basic concurrency primitives in .Net?

Specifically I want to use a Check and Set operator.

AakashM
  • 62,551
  • 17
  • 151
  • 186
Keynan
  • 1,338
  • 1
  • 10
  • 18
  • 5
    Not sure what check and set is, but look into the `Interlocked` class. It has compare-and-exchange and similar operations. – CodesInChaos Nov 24 '11 at 16:41

2 Answers2

3

You are probably looking for Interlocked.CompareExchange.

Tudor
  • 61,523
  • 12
  • 102
  • 142
3

You need to look at the Interlocked class in the System.Threading namespace. The CompareExchange is the method you're looking for.

It has the form CompareExchange(target, value, comparand) which in pseudo-code means if(target==comparand) target=value;.

There are also a load of other atomic methods on the Interlocked class that are useful, such as Increment, Decrement, Add and Exchange.

Sean
  • 60,939
  • 11
  • 97
  • 136