Debug multithreaded applications in Visual Studio
Applies to: Visual Studio Visual 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.
Visual Studio provides different tools for use in debugging multithreaded apps.
For threads, the primary tools for debugging threads are the Threads window, thread markers in source windows, the Parallel Stacks window, the Parallel Watch window, and the Debug Location toolbar. To learn about the Threads window and Debug Location toolbar, see Walkthrough: Debug using the Threads window. To learn how to use the Parallel Stacks and Parallel Watch windows, see Get started debugging a multithreaded application. Both topics show how to use thread markers.
For code that uses the Task Parallel Library (TPL) or the Concurrency Runtime, the primary tools for debugging are the Parallel Stacks window, the Parallel Watch window, and the Tasks window, which also supports JavaScript. To get started, see Walkthrough: Debugging a parallel application and Walkthrough: Debugging a C++ AMP application.
For debugging threads on the GPU, the primary tool is the GPU Threads window. See How to: Use the GPU Threads window.
For processes, the primary tools are the Attach to Process dialog box, the Processes window, and the Debug Location toolbar.
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.
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.
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.