Search code examples
c#.netconcurrencyclojureatomic

C# atom reference type equivalent to clojure atom


Is there a C# BCL equivalent to Clojure atom, something like Atom<T> that provides atomic swap/reset semantics for reference type T ? .NET has immutable collections so so I could see using this class just like in Clojure.


Solution

  • C# has the Interlocked class:

    https://msdn.microsoft.com/en-us/library/system.threading.interlocked(v=vs.110).aspx

    you can do swaps with Interlocked.CompareExchange generically:

    https://msdn.microsoft.com/en-us/library/bb297966(v=vs.110).aspx