Just ran into a program where += is used on a shared variable among threads, so is += thread safe, i.e. performs addition and assignment atomically?
No it isn't thread safe since it's equivalent to:
int temp = orig + value; orig = temp;
You can use Interlocked.Add instead:
Interlocked.Add
Interlocked.Add(ref orig, value);
1.4m articles
1.4m replys
5 comments
57.0k users