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?
See Question&Answers more detail:osJust ran into a program where += is used on a shared variable among threads, so is += thread safe, i.e. performs addition and assignment atomically?
See Question&Answers more detail:osNo it isn't thread safe since it's equivalent to:
int temp = orig + value;
orig = temp;
You can use Interlocked.Add
instead:
Interlocked.Add(ref orig, value);