Trick for debugging parallel code

Thanks to Microsoft’s TPL (Task Parallel Library) writing parallel applications has become easy. In some cases it’s as easy as swapping a foreach loop for a Parallel.ForEach loop. The one thing that it makes harder though is debugging! How are you supposed to keep track of which thread you are in? When stepping through code the debugger jumps around to different lines because multiple threads are running code at the same time. It’s frustrating. Here is a simple solution I found by only letting one thread run at a time while debugging. Use ParallelOptions and a debug preprocessor! Example:

var items = new[] {1, 2, 3}; var options = new ParallelOptions(); #if DEBUG options.MaxDegreeOfParallelism = 1; #endif Parallel.ForEach(items, options, item => { //do something });

The best part is you don’t have to change anything when you do a release build. The preprocessor takes care of not compiling line 5 and so the default MaxDegreeOfParallelism value will be used in production.