Windows System Programming Third Edition [Electronic resources]

Johnson M. Hart

نسخه متنی -صفحه : 291/ 116
نمايش فراداده

  • Thread States

    Figure 7-4, which is taken from Custer's Inside Windows NT, page 210 (also see Solomon and Russinovich's updated version of this book), shows how the executive manages threads and shows the possible thread states. This figure also shows the effect of program actions. Such state diagrams are common to all multitasking OSs and help clarify how a thread is scheduled for execution and how a thread moves from one state to another.

    Figure 7-4. Thread States and Transitions (From Inside Windows NT, Copyright © 1993, by Helen Custer. Copyright Microsoft Press. Reproduced by permission of Microsoft Press. All rights reserved.)

    Chapter 8. I/O operations will also wait for completion of a disk or other data transfer, and numerous other functions can cause waiting. It is common to say that a thread is blocked, or sleeping, when in the wait state.

  • A thread is ready if it could be running. The executive's scheduler could put it in the running state at any time. The scheduler will run the highest-priority ready thread when a processor becomes available, and it will run the one that has been in the ready state for the longest time if several threads have the same high priority. The thread moves through the standby state.

  • Normally, as described above, the scheduler will place a ready thread on any available processor. The programmer can specify a thread's processor affinity (see Chapter 9) by giving the processors on which a thread is to be run. In this way, the programmer can allocate processors to threads. The appropriate functions are SetProcessorAffinityMask and GetProcessorAffinityMask. SetThreadIdealProcessor can be used to specify a preferred processor that the scheduler will use whenever possible.

  • The executive will move a running thread to the ready state if the thread's time slice expires without the thread waiting. Executing Sleep(0) will also move a thread from the running state to the ready state.

  • The executive will place a waiting thread in the ready state as soon as the appropriate handles are signaled, although the thread actually goes through an intermediate transition state. It is common to say that the thread wakes up.

  • There is no way for a program to determine the state of another thread (of course, a thread, if it is running, must be in the running state, so it would be meaningless for a thread to find its own state). Even if there were, the state might change before the inquiring thread would be able to act on the information.

  • A thread, regardless of its state, can be suspended, and a ready thread will not be run if it is suspended. If a running thread is suspended, either by itself or by a thread on a different processor, it is placed in the ready state.

  • A thread is in the terminated state after it terminates and remains there as long as there are any open handles on the thread. This arrangement allows other threads to interrogate the thread's state and exit code.