Threadsafe Incrementing in C#

added by Paul Wheeler
7/11/2014 3:33:02 PM

4 Kicks, 135 Views

Recently I’ve had the opportunity to review a number of different parallel C# methods that were performing work over a collection of items. Nearly all of them have used ++ to increment parent or global variables from inside threaded contexts. Unfortunately the ++ operator is not guaranteed to be threadsafe in C#. So let’s take a simple code example using ++ and convert it to a threadsafe one.


0 comments