Question

I've seen a couple of places quoting the following bit of the C# spec: "Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write." Can somebody point me to these library functions?

Was it helpful?

Solution

The Interlocked class should provide you with what you are looking for; such as Increment and Decrement.

OTHER TIPS

I think it's referring to functions such as Interlocked.CompareExchange.

This method can be used to, e.g., atomically update a double:

static void Add(ref double field, double amount)
{
    double before, after;
    do
    {
        before = field;
        after = before + amount;
    }
    while (Interlocked.CompareExchange(ref field, after, before) != before);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top