Debug multi-threaded applications in Visual Studio

Source: Internet
Author: User
Debug multi-threaded applications in Visual Studio

10.3.3 debug multi-threaded applications

A multithreaded application is an application that has more than one thread running in a given process. By default, each process running an application has at least one execution thread. You may create multiple threads for parallel processing. This can significantly improve performance, especially when running today's multi-core processor and super-thread technology. However, multithreading also brings more overhead. Code becomes more complex and difficult to write and debugging. If you have written multi-threaded applications, you will know.

Fortunately, Visual Studio provides several tools to make the job easier. We will not describe how to write multi-threaded applications here. This section describes the debugging options available for debugging multi-threaded applications. These tools and features are listed below.

View the thread in the source code during the debugging session;

The debug location toolbar for viewing processes, threads, and marked threads;

The thread window used to process the thread list in the application;

Allows you to set breakpoint filters for individual threads.

Let's take a closer look at each of these features.

1. Discover and Mark threads

Visual Studio allows you to visualize threads in application debugging mode. When you stop a breakpoint, the application is paused. You can debug the active thread. At this time, other threads in the application still exist. However, they may be invisible. To view them in the Debug menu, use the show threads in soure (display thread in source code) option from the Debug menu, as shown in 10-37.

Select this option to highlight other threads in the code. During the debugging session, the code will highlight the blank edge of the indicator in the code window. The icon used to highlight these items looks like two wavy lines (or decorative lines ). Figure 10-38 provides an example.

 
Figure 10-37 in source code)
Show/hide threads in Options
 
Figure 10-38 threads highlighted at the blank edge of the indicator

Note: The image on the left side of the 29th rows. It indicates that there is a thread at this position in your source code. Hover the mouse over this indicator to display the threads referenced by the indicator (or multiple threads ). Each thread is represented by its ID (in square brackets) and name (if any. Figure 10-39 provides an example.

The prompt named threads help you better identify them during debugging. To name a thread, you can use the threading namespace. Specifically, call the name attribute of the thread class.

Now that you have found a thread, you may want to mark it for further monitoring. This only helps to group the threads you want to monitor and those you don't care about. You can mark a thread on the right side of the blank edge of the indicator. Right-click the indicator and select the flag option in the context menu to complete this operation. Figure 10-40 provides an example.

 
Figure 10-39 hover over the thread indicator
 
Figure 10-40 indicates a thread

You can also unmark the thread. You can also mark the thread directly in the tbread window. The marked thread provides special groups in the tbread window and the debug location toolbar. Next we will introduce these features.

2. Manage Debugging Processes and threads

You can use the debug location toolbar to switch between the processes being debugged and the threads in these processes. This toolbar is 10-41. The process list is displayed on the left. Here you can select a process to view detailed information about the process, including the thread being executed. However, many multi-threaded applications run in a single process.

 
(Click to view the larger image) Figure 10-41 debug location Toolbar

The thread list on the toolbar displays the list of threads of the selected process. This list is highlighted in Figure 10-41. Note that threads are displayed along with their IDs, names, and Mark indicators. You can select a thread in the list to jump to the Source Code related to the thread. If there is no Source Code related to the selected thread, the thread name will be displayed in red when selected. You can filter the list by switching the second button on the right side of the thread list (with two tags displayed) to display only the marked threads. The first button on the right only marks (or unmarks) the current active thread. Finally, the call stack is displayed in the list on the right. The active stack frame in the call stack is shown in the debugging window (Monitoring window, partial window, automatic window, etc.

You can also manage threads in the threads window (Debug menu, windows, threads. Here you will see all the threads listed for the given process. Figure 10-42 provides an example. Note: The Marked threads are displayed on the left of the list. Here we have two marked threads in the sample application. Also note that these threads have been named, so it is easy to identify in the name column.

 
(Click to view the larger image) Figure 10-42 threads window

The threads window has several available options. They are displayed in the context menu in Figure 10-42. Note the switch to thread option. It allows you to switch to the active thread being debugged. The active thread is indicated by a yellow arrow in the thread list (on the right side of the mark. Switching the active thread will change the debugging context and content in the debugging window. You can also use the context menu to freeze (or pause) threads. This is equivalent to suspending a thread.

3. Special Thread interruption

You can also decide to interrupt the code line based on the calling thread. To do this, set a breakpoint and select a breakpoint filter (discussed earlier ). Figure 10-43 provides an example. You can choose to interrupt the thread Based on the thread ID or its name. In this case, the breakpoint is hit based on the next option. Of course, this requires you to name the thread in the code.

 
Figure 10-43 set a breakpoint Filter Based on a thread

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.