Friday, July 1, 2011

Reblog: +=1, -=1 not atomic operations at « George Birbilis @zoomicon

This is something that bit me sometime ago: seems +=1 and -=1 aren’t atomic operations at and other .NET languages (not sure for C++) although modern CPUs have INC and DEC instructions at least for integers (maybe IL – intermediate language [for .NET compilers' target "theoretical" machine spec] – doesn’t define such?)

You have to use the class System.Threading.Interlocked and specifically the Increment and Decrement methods it has...