Waiting
. A thread can enter the waiting state in several ways: a thread can voluntarily wait for an object to synchronize its execution, the operating system can wait on the thread’s behalf (such as to resolve a paging I/O), or an environment subsystem can direct the thread to suspend itself. When the thread’s wait ends, depending on the priority, the thread either begins running immediately or is moved back to the ready state.Transition
. A thread enters the transition state if it is ready for execution but its kernel stack is paged out of memory. Once its kernel stack is brought back into memory, the thread enters the ready state.Terminated
. When a thread finishes executing, it enters the terminated state. Once the thread is terminated, the executive thread object (the data structure in a nonpaged pool that describes the thread) might or might not be deallocated. (The object manager sets the policy regarding when to delete the object.)Initialized
. This state is used internally while a thread is being created.Table 5-4 describes the state transitions for threads, and Figure 5-16 illustrates a simplified version. (The numeric values shown represent the value of the thread-state performance counter.) In the simplified version, the Ready, Standby, and Deferred Ready states are represented as one. This reflects the fact that the Standby and Deferred Ready states act as temporary placeholders for the scheduling routines. These states are almost always very short-lived; threads in these states always transition quickly to Ready, Running, or Waiting. More details on what happens at each transition are included later in this section.
Init
Ready
Running
Standby
Terminated
Waiting
Transition
Deferred Ready
Init
A thread becomes Initialized during the first few moments of its creation (
Ready
A thread is added in the dispatcherready database of its ideal processor.
Running
Selected by
Picked up for execution by local CPU
Preemption after wait satisfaction
Standby
Selected by
Selected
Terminated
Killed before
Killed
A thread can kill only itself. It must be in the Running state before entering KeTerminateThread.
Waiting
Thread enters a wait
Only running threads can wait.
Transition
Kernel stack no longer resident
Only waiting threads can transition.
Deferred Ready
Last step in
Affinity change
Thread becomes preempted (if old processor is no longer available)
Affinity change
Wait satisfaction (but no preemption)
Kernel stack swap-in completed
EXPERIMENT: Thread-Scheduling State Changes
You can watch thread-scheduling state changes with the Performance tool in Windows. This utility can be useful when you’re debugging a multithreaded application and you’re unsure about the state of the threads running in the process. To watch thread-scheduling state changes by using the Performance tool, follow these steps:
Run Notepad (Notepad.exe).
Start the Performance tool by selecting All Programs from the Start menu and then selecting Performance Monitor from the Administrative Tools menu. Click on the Performance Monitor entry under Monitoring Tools.
Select the chart view if you’re in some other view.
Right-click on the graph, and choose Properties.
Click on the Graph tab, and change the chart vertical scale maximum to 7. (As you’ll see from the explanation text for the performance counter, thread states are numbered from 0 through 7.) Click OK.
Click the Add button on the toolbar to bring up the Add Counters dialog box.
Select the Thread performance object, and then select the Thread State counter. Select the Show Description check box to see the definition of the values:
In the Instances box, select
Scroll back up in the Instances box to the
Now close the Add Counters dialog box by clicking OK.