Debug multithreaded applications in Visual Studio

Applies to: yesVisual Studio noVisual Studio for Mac

Observação

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

A thread is a sequence of instructions to which the operating system grants 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 several simultaneous threads. Parallel processing using many threads can greatly improve program performance, but it may also make debugging more difficult because you're tracking many threads.

Multithreading can introduce new types of potential bugs. For example, two or more threads may need to access the same resource, but only one thread at a time can safely access the resource. Some form of mutual exclusion is necessary to make sure that only one thread is accessing the resource at any time. If mutual exclusion is implemented incorrectly, it can create a deadlock condition where no thread will execute. Deadlocks are often a hard problem to debug.

Tools for debugging multithreaded apps

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

Visual Studio also provides powerful breakpoints and tracepoints, which can be useful when you debug multithreaded applications. Use breakpoint conditions and filters to place breakpoints on individual threads. Tracepoints enable you to trace execution of your program without breaking, to study problems such as deadlocks. For more information, see Breakpoint actions and tracepoints.

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

Articles about debugging multithreaded apps

Get started debugging a multithreaded application

A tour of thread debugging features, emphasizing features in the Parallel Stacks window and the 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

Several 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.

See also