Atomic read-modify-write in C #

I saw a couple of places citing the following bit of the C # specification: "In addition to the library functions designed for this purpose, there is no guarantee of atomic read-modify-write." Can someone point me to these library functions?

+3
source share
2 answers

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

+4
source

I think this applies to features like Interlocked.CompareExchange.

This method can be used, for example, to 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);
}
+3
source

Source: https://habr.com/ru/post/1784809/


All Articles