Share via


Threads and Processes

Threads and processes are related concepts in computer science. Both represent sequences of instructions that must execute in a specific order. Instructions in separate threads or processes, however, can execute in parallel.

Processes exist in the operating system and correspond to what users see as programs or applications. A thread, on the other hand, exists within a process. For this reason, threads are sometimes referred to as light-weight processes. Each process consists of one or more threads.

The existence of multiple processes enables a computer to perform more than one task at a time. The existence of multiple threads enables a process to separate work to be performed in parallel. On a computer with multiprocessors, processes or threads can run on different processors. This enables true parallel processing.

Perfect parallel processing is not always possible. Threads sometimes must be synchronized. One thread may have to wait for a result from another thread, or one thread may need exclusive access to a resource that another thread is using. Synchronization problems are a common cause of bugs in multithreaded applications. Sometimes threads may end up waiting for a resource that never becomes available. This results in a condition called deadlock.

The Visual Studio debugger provides powerful but easy-to-use tools for debugging threads and processes.

Tools for Debugging Threads and Processes in Visual Studio

The primary tools for working with processes in Visual Studio are the Attach to Process dialog box, the Processes window, and the Debug Location toolbar. The primary tools for debugging threads are the Threads window, thread markers in source windows, and the Debug Location toolbar.

The following table shows the information available and the actions you can perform in each of these places:

User Interface

Information Available

Actions You Can Perform

Attach to Process dialog box

Available Processes you can attach to:

  • Process name (.exe)

  • Process ID number

  • Menubar Title

  • Type (managed, x86, x64, IA64)

  • User Name (account name)

  • Session number

Select a process to attach to.

Select a remote computer.

Change transport type for connecting to remote computers

Processes window

Attached Processes:

  • Process Name

  • Process ID number

  • Path to process .exe

  • Menubar Title

  • State (Break. Running)

  • Debugging (Native, Managed, and so on.)

  • Transport type (default, native with no authentication, smart devices)

  • Transport Qualifier (remote computer)

Tools:

  • Attach

  • Detach

  • Terminate

Shortcut menu:

  • Attach

  • Detach

  • Detach when debugging stopped

  • Terminate

Threads window

Threads in current process:

  • Thread ID

  • Category (main thread, interface thread, remote procedure call handler, or worker thread)

  • Thread Name

  • Location where thread is created

  • Priority

  • Suspend level

Shortcut menu:

  • Switch to a thread

  • Freeze a running thread

  • Thaw a frozen thread

  • Flag a thread for additional study

  • Unflag a thread

  • Rename a thread

  • Show and hide threads

Other actions:

  • View the call stack for a thread in a DataTip

Source window

Thread indicators in left gutter indicate single or multiple threads

Shortcut menu:

  • Switch to a thread.

  • Flag a thread for additional study

  • Unflag a thread

Debug Location toolbar

  • Current process

  • Current thread

  • Current stack frame

  • Switch to another process

  • Switch to another thread in current process

  • Switch to another stack frame in current thread

  • Flag or unflag current thread

  • Show only flagged threads

See Also

Tasks

How to: Use the Processes Window

Other Resources

Attaching to Running Processes

Debugging Multithreaded Applications