This C# class simply provides a counter-value, which may be set, incremented, and decremented. Increment and Decrement are both thread-safe operations.

using System.Threading;

namespace Utils
{
    /// <summary>
    /// This class simply provides a counter-value, which may be set,
    /// incremented, and decremented. Increment and Decrement are both
    /// thread-safe operations.
    /// </summary>
    internal sealed class AtomicCounter
    {
        private int _value;

        /// <summary>
        /// Create a new AtomicCounter object with an initial
        /// counter-value of zero.
        /// </summary>
        public AtomicCounter()
        {
            _value = 0;
        }

        /// <summary>
        /// Assign the given amount to the counter-value.
        /// </summary>
        /// <param name="amount">
        /// the integer value to set the counter to
        /// </param>
        public void Set(int amount)
        {
            _value = amount;
        }

        /// <summary>
        /// Add the given amount to the counter-value, in an atomic
        /// thread-safe manner.
        /// </summary>
        /// <param name="amount">
        /// the integer amount to add to the counter-value
        /// </param>
        public void Increment int amount)
        {
            Interlocked.Add(ref _value, amount);
        }

        /// <summary>
        /// Subtract the given amount from the counter-value, in an
        /// atomic thread-safe manner.
        /// </summary>
        /// <param name="amount">the integer amount to subtract from to
        /// the counter-value (default value is 1)</param>
        /// <returns>the new value of the counter</returns>
        public int Decrement(int amount = 1)
        {
            return Interlocked.Add(ref _value, amount * -1);
        }
    }
}