Using the Threads Window
With the Threads window, you can examine and control threads in the program you are debugging. A thread is a sequence of executable instructions created by a program. By default, a program has a single thread. Multithreaded programs create additional threads. One thread is active at a time. The active thread is the thread that is currently able to run.
To display the Threads window
- From the Debug menu, choose Windows and click Threads. (The debugger must be running or in break mode.)
From the Threads window, you can set the active thread. In addition, you can freeze or thaw the execution of each individual thread. Freezing prevents the execution of a thread. Thawing allows it to continue. "Frozen" and "thawed" are states set by the debugger.
To set an active thread
- In the Threads window, right-click a thread and choose Switch to Thread from the context menu.
- On the Debug Location toolbar, select the Program list box and choose the program you want to make current.
- In the Threads window, double-click the thread to make it active.
A yellow arrow identifies the active thread.
To freeze (prevent execution of) a thread
- In the Threads window, right-click a thread and choose Freeze from the shortcut menu.
Two vertical blue bars identify a frozen thread.
To thaw (allow execution of) a thread
- In the Threads window, right-click a thread and choose Thaw from the shortcut menu.
The absence of vertical blue bars identifies a thawed or unfrozen thread.
You can suspend or resume thread execution using calls to SuspendThread and ResumeThread. In MFC, you can use CWinThread::SuspendThread and CWinThread::ResumeThread.
These calls change the Suspend count shown in the Threads window. To execute, a thread must be unfrozen and have a Suspend count of zero. You cannot change the Suspend count or resume a suspended thread from the Threads window.
Tips for Debugging Threads
- You can set the thread name in native code using the SetThreadName API or in managed code using the Thread.Name property. For more information, see Setting a Thread Name (Managed) or Setting a Thread Name (Unmanaged). Thread names are especially useful for managed code, since you cannot identify threads by Thread ID in managed code.
- Deadlocks in multithreaded applications are a particularly nasty type of bug.
- When debugging native code, you can view the contents of the Thread Information Block by entering @TIB in the Watch window or QuickWatch dialog box.
- When debugging native code, you can view the last error code for the current thread (the value returned by a call to GetLastError) by entering @Err in the Watch window or QuickWatch dialog box.
- When debugging mixed code with calls from native to managed code, the managed code runs in the same physical thread as the native code that called it. Suspending or freezing the native thread will also freeze the managed code.
- You can use CRT functions for debugging a multithreaded application. For more information, see _malloc_dbg.