VS2010: Parallel programming

 By Tim Anderson

New features in Visual Studio 2010 make multi-threaded code easier.

HardCopy Issue: 52 | Found In: Visual Studio 2010 | Published: 19/05/2011 | Last Revision: 19/05/2011

Writing and debugging multi-threaded code is challenging, but necessary if you are to take advantage of today’s multi-core computers. Supporting concurrency is a strong theme in Visual Studio 2010, both for managed (see Inside .NET 4.0 on page 8) and for native code.

Visual C++ 2010

There are several themes in Visual C++ 2010. At the compiler and library level, there is the inclusion of a number of features from the emerging C++Ox standard, which is a major revision of the language. C++0x is not yet final (0x is meant to be a year number and the insider joke is to think of it as hexadecimal) but is nearing completion. Its aim is to make C++ easier to learn as well as improving its capability. According to Bjarne Stroustrup, creator of C++, “The range of abstractions that C++ can express elegantly, flexibly, and at zero costs compared to hand-crafted specialised code has greatly increased.”

Visual C++ 2010 is not a full implementation of C++ 0x but does include:
  • lambda expressions,
  • static (compile-time) assertions,
  • the auto keyword for type inference and decltype which gets the type of an expression,
  • nullptr to denote the null pointer (although Microsoft recommends using the Microsoft-specific __nullptr in native code and nullptr in managed code),
  • rvalue references, which can make move assignments more efficient,
  • long long for a 64-bit integer.

There are also parts of the C++ 0x standard library in Visual C++ 2010, including forward_list, which is a single-linked list, and tuple. On the tools side, Visual C++ projects now use MSBuild, as used by the other Visual Studio languages.

A core concept in this context is the task, a unit of work represented in C++ by a lightweight class called task handle, and in .NET by System.Threading.Task. A task is not a thread as the duty of assigning tasks to threads is taken care of by the framework. A task group is what its name implies: a collection of tasks. The Parallel Patterns Library enables C++ programmers to take advantage of tasks, either implicitly or explicitly, as part of the new C++ concurrency runtime which schedules and manages them. The .NET equivalent is the Task Parallel Library.

The Concurrency Runtime

The Concurrency Runtime is a framework for parallel programming in C++ and uses many of the new C++ 0x features internally. It has four components. The Parallel Patterns Library (PPL) lets you create concurrent applications by dividing work into tasks that can execute in parallel. It also offers general-purpose parallel algorithms, including parallel_for and parallel_invoke. The PPL uses the services of the Task Scheduler (see below). The Asynchronous Agents Library contains templates that enable multiple operations to handle synchronisation by exchanging messages. The Task Scheduler schedules tasks across available resources, and is configurable through scheduler policies. The Resource Manager abstracts computer resources to support the Task Scheduler, or your own concurrency library.

Tasks and Stacks

The concurrent features in Visual Studio 2010 are supported by two new debugging windows which work with both managed and native code. The Parallel Tasks window lists the active tasks in your application, showing their status (such as Running, Waiting or Scheduled), their location and their thread assignment. Double-clicking a task takes you to its code. The Parallel Stacks window presents a number of graphical views of the call stack. The Tasks view shows the call stack from a task perspective, with threads that are not running tasks hidden. The current code path is shown by blue arrows and highlights, with a yellow arrow on the currently active task. Tooltips show the code and line number of each method. The Threads view expands the call stack to show all the threads in the call stack, typically a much larger and more complex picture. Again, the current code path is shown in blue. The underlying WPF graphics are used to advantage, with a sliding zoom control and an optional thumbnail view for navigating.

Parallel Stacks window screenshot
The Parallel Stacks window in Threads view with the current code path in blue.

The Parallel Tasks and Parallel Stacks windows give excellent insight into how your concurrent application is executing and are easier to use than the generic Threads debug window. Of course these new views only make sense if you are using the new parallel library in .NET or C++.

Parallel profiling

The Visual Studio profiler has new features for profiling parallel applications. A wizard guides you to select the concurrency profiling method with an option to visualise the behaviour of a multi-threaded application. Running the analysis generates a report with three views: CPU utilisation, Threads and Cores. You can see to what extent the application used multiple cores, how many threads executed, and what each thread was doing during the course of the application. When you need to fine-tune a concurrent application, these new reports offer a lot of value.

Share and Bookmark  

Comments

Be the first to comment about this article...

Leave a comment

You must login to place comments.

Skin Border Image

Related Links

Skin Border Image