Debug Multithreaded Applications in Visual Studio

A thread is a sequence of instructions to which the operating system allocates processor time. Every process that is running in the operating system consists of at least one thread. Processes that have more than one thread are called multithreaded.

Computers with multiple processors, multi-core processors, or hyperthreading processes can run multiple threads at the same time. Parallel processing of multiple threads can greatly improve program performance, but it can also make debugging more difficult because it introduces the need to keep track of multiple threads.

In addition, multithreading introduces some new types of potential bugs. Often, for example, two or more threads have to access the same resource, but only one thread can safely access the resource at a time. Some form of mutual exclusion is necessary to make sure that only one thread is accessing the resource at a time. If mutual exclusion is performed incorrectly, it can create a deadlock condition where no thread can execute. Deadlocks can be a particularly hard problem to debug.

Visual Studio provides different tools for use in debugging multithreaded apps.

Visual Studio also provides powerful breakpoints and tracepoints, which can be very useful when you debug multithreaded applications. You can use breakpoint conditions and filters to place breakpoints on individual threads. See Using Breakpoints.

Debugging a multithreaded application that has a user interface can be especially difficult. In that case, you might consider running the application on a second computer and using remote debugging. For information, see Remote Debugging.

In This Section

Get started debugging a multithreaded application.
A guided tour of thread debugging features, with emphasis on features in the Parallel Stacks window and Parallel Watch window.

Tools for Debugging Threads and Processes
Lists the features of the tools for debugging threads and processes.

Debug Multiple Processes
Explains how to debug multiple processes.

Walkthrough: Debug using the Threads Window.
Walkthrough that shows how to use the Threads window and the Debug Location toolbar.

Walkthrough: Debug a Parallel Application
Walkthrough that shows how to use the Parallel Stacks and Tasks windows.

How to: Switch to Another Thread While Debugging
Three ways to switch the debugging context to another thread.

How to: Flag and Unflag Threads
Mark or flag threads that you want to give special attention to while debugging.

How to: Debug On a High-Performance Cluster
Techniques for debugging an application that runs on a high-performance cluster.

Tips for Debugging Threads in Native Code
Simple techniques that can be useful for debugging native threads.

How to: Set a Thread Name in Native Code
Give your thread a name that you view in the Threads window.

How to: Set a Thread Name in Managed Code
Give your thread a name that you view in the Threads window.

Using Breakpoints

  • Use breakpoint conditions or filters when you want to debug an individual thread.

  • Tracepoints enable you to trace execution of your program without breaking. This can be useful for studying problems such as deadlocks.

    Threading concepts in .NET Framework programming, including example code.

    Multithreading in Components
    How to use multithreading in .NET Framework components.

    Multithreading Support for Older Code (Visual C++)
    Threading concepts and example code for C++ programmers using MFC.

See Also

Debug Threads and Processes
Remote Debugging