I would think in most cases you would still want an atomic test and set. As
many times you want to know the value before setting it - atomically.
Otherwise you will need a read outside of the interlocked, which could
result in a race we are trying to avoid. So I think you still need two
methods to round the api out:
Interlocked.Exchange<bool>(ref value, T value) // unconditional set.
Interlocked.CompareExchange<bool>(ref T value, T comp, T value); //
conditional set.
So you could:
private bool started = true;
public void Stop()
{
// Set started to false if true. Set exactly once.
if ( Interlocked.CompareExchange<bool>(ref this.started, false,
true) )
return;
throw new Exception("Already stopped");
}
--
William Stacey [C# MVP]
| >That would work if those where exposed in the framework. It would be a
| >simple thing (I would think) for them to wrap that, however, and expose a
| >typed bool api.
|
| Or, for Interlocked.Xor, you could use Interlocked.Increment and
| examine only the lowest bit...
|
| --
| Lucian