Thread Synchronization

public sealed class ReentranceGuard
{
    private int executionStatus = 0;

    public int ExecutionStatus => this.executionStatus;

    /// <summary>
    /// Returns true if no other thread has called <see cref="TryEnter"/> without calling <see cref="Exit"/>
    /// Note that the caller takes responsibility for calling Exit anytime TryEnter returns true,
    /// or all subsequent calls to TryEnter will return false.
    /// </summary>
    public bool TryEnter()
    {
        return 0 == Interlocked.CompareExchange(ref this.executionStatus, 1, 0);
    }

    /// <summary>
    /// Exits the timer callback by resetting the status. Call this in a finally clause when
    /// (and only when) TryEnter returns true.
    /// </summary>
    public void Exit()
    {
        Interlocked.Exchange(ref this.executionStatus, 0);
    }
}

Interlocked.MemoryBarrier 1
Interlocked.MemoryBarrierProcessWide 2
AsymmetricLock gist by jkotas 3
Add Interlocked.MemoryBarrierProcessWide method 4
Interlocked.CompareExchange 5
LogoFX ReentranceGuard.cs 6
Interlocked.CompareExchange missing for uint, ulong and general structs 7

< «