Files
codeql/csharp/ql/src/Documentation/XmldocExample.cs
2018-08-02 17:53:23 +01:00

36 lines
1.0 KiB
C#

using System;
using System.Threading;
/// <summary>
/// A minimal threadsafe counter.
/// </summary>
class AtomicCounter
{
/// <summary>
/// The current value of the counter.
/// </summary>
int currentValue = 0;
/// <summary>
/// Increments the value of the counter.
/// </summary>
///
/// <param name="incrementBy">The amount to increment.</param>
/// <exception cref="System.OverflowException">If the counter would overflow.</exception>
/// <returns>The new value of the counter.</returns>
///
/// <remarks>This method is threadsafe.</remarks>
public int Increment(int incrementBy = 1)
{
int oldValue, newValue;
do
{
oldValue = currentValue;
newValue = oldValue + incrementBy;
if (newValue < 0) throw new OverflowException("Counter value is out of range");
}
while (oldValue != Interlocked.CompareExchange(ref currentValue, newValue, oldValue));
return newValue;
}
}