Channels ▼

Gastón Hillar

Dr. Dobb's Bloggers

New Parallel Debugging Windows in Visual Studio 2010 Beta 1

August 03, 2009

Visual Studio 2010, still in Beta 1, offers two new debugging windows: Parallel Stacks and Parallel Tasks. Using them, it is easier to understand the new Parallel Extensions offered by .NET 4.

In my previous post Tasks Are Not Threads in .NET 4 Beta 1, I began talking about the differences between tasks and threads when working with .NET 4.

The easiest way to understand the relationship between the new tasks and the underlying threads in .NET 4 is working with simple parallelized code snippets and running them step-by-step using the new debugging windows.

I'm going to use a very simple C# Windows Forms application to explain the new possibilities offered by Visual Studio 2010 to debug .NET 4 parallelized code. In order to keep the example simple, I'm going to use very simple code. It's main purpose is to explain the information shown by these new debugging windows. It doesn't represent a best practice. It is just code added to a Form class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// Added for Threading (ParallelOptions), Tasks, and Concurrent Collections
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace WindowsFormsApplication2
{
   public partial class Form1 : Form
   {
      public Form1()
   {
        InitializeComponent();
   }
   private ConcurrentQueue<double> _results;
   private void button1_Click(object sender, EventArgs e)
   {
       int maxIterations = 500000000;
       _results = new ConcurrentQueue<double>();
       var parallelOptions = new ParallelOptions();
       parallelOptions.MaxDegreeOfParallelism = 4;
       Parallel.For(1, maxIterations, parallelOptions, i =>
      {
          AddProbability(CalculateProbability(i));
      });
   }
   private double CalculateProbability(int probabilityIteration)
   {
      // Do something that takes a long time
      return (Math.Sqrt((double)probabilityIteration) * Math.Sqrt((double) probabilityIteration));
   }
   private void AddProbability(double probability)
   {
       _results.Enqueue(probability);
       System.Console.WriteLine(probability.ToString());
   }
  }
}

The code uses the new System.Threading.Collections.ConcurrentQueue to store double results. A ConcurrentQueue represents a variable size first-in-first-out (FIFO) collection. It is possible to add and remove items from this concurrent collection letting Parallel Extensions manage the necessary low-level coordination stuff.

The code programmed in the Click event handler creates a new ParallelOptions instance, assigning 4 to the MaxDegreeOfParallelism property. Again, this is not a best practice. However, it allows you to check what happens with different degrees of parallelism and check it using the new debugging features. You have to understand how things work in the background to create efficient designs. Remember, tasks aren't threads.

Then, it runs a Parallel.For from 1 to maxIterations. The code just uses some processing power and enqueues an output to the console. Again, this is not a best practice for parallelized code. However, it allows you to understand what's going on in the background. For example, this way, you will learn that Parallel.For creates many tasks. Parallel.For uses many task instances to parallelize loops. These tasks steal work from many threads according to the number of available logical cores (hardware threads).

If you insert a breakpoint in the line System.Console.WriteLine(probability.ToString());, as shown in the following picture:

You will be able to see many tasks running in parallel, according to the parameters specified for the ParallelOptions instance. You just have to select Debug, Windows, Parallel Tasks (Ctrl + Shift + D, K). The IDE will show a new Window (Parallel Tasks), displaying a list of all the tasks and their status (scheduled, running, waiting, waiting-deadlocked, etc.) as shown in the following picture:

You can check what's going on with each different concurrent task. Now, you have similar possibilities than the ones offered by previous Visual Studio versions with threads. However, the information is better, because you can check whether a task is scheduled or waiting-deadlocked. You can order and group the information shown in the windows, as any other Visual Studio feature.

What about underlying threads? Where are the threads that support the tasks' execution? The Parallel Tasks grid shows a column, Thread Assignment. This number is the id shown in the Threads Windows. Hence, you know which managed thread is supporting the execution of a certain task.

However, there is a simpler way to see what's going on between tasks and threads. You just have to select Debug, Windows, Parallel Stacks (Ctrl + Shift + D, S). The IDE will show a new Window (Parallel Stacks), displaying a diagram with all the tasks or threads, their status and their relationship, as shown in the following picture:

It offers a very interesting view of what's going on in your concurrent code. 1 thread doing this, 3 threads doing that, etc.

Besides, you can change the value for the list box in the upper left corner from Threads to Tasks and the IDE will display a diagram with all the tasks, their status and their relationship, as shown in the following picture:

There is a great difference between the Parallel Extensions CTP (Community Technology Preview) and the Parallel Extensions included in .NET 4 with Visual Studio 2010 Beta 1. The last one added very interesting concurrent debugging capabilities. Now, you can see what's going on with tasks and threads, their relationships and their status.

These new debugging windows allow developers to understand concurrent and parallel programming. These new debugging features show what's going on behind the scenes.

For more information about Concurrent Collections in .NET 4 Beta 1, you can read an introductory article on this topic: Concurrent Collections for C# Using Parallel Extensions.

For more information about these new debugging features, you can read this nice post written by Daniel Moth, including a link to a video on the topic.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video