Simple wrapper around Interlocked.CompareExchange() to implement TestAndSet().
$ dotnet add package B.AtomicsImplements atomic TestAndSet operations.
Implemented as a super-simple wrapper around Interlocked.CompareExchange.
using B.Atomics;
...
int isRunning = 0;
if(Atomic.TestAndSet(ref isRunning, replaceWithValue: 1, testForValue: 0))
{
// run
isRunning = 0;
}
Also see unit tests for examples.