Timed Waits
The final function, Sleep, allows a thread to give up the processor and move from the running to the wait state for a specified period of time. A thread can, for example, perform a task periodically by sleeping after carrying out the task. Once the time period is over, the scheduler moves the thread back to the ready state. A program in Chapter 11 (Program 11-4) uses this technique.
VOID Sleep (DWORD dwMilliseconds)
The time period is in milliseconds and can even be INFINITE, in which case the thread will never resume. A value of 0 will cause the thread to relinquish the remainder of the time slice; the kernel moves the thread from the running state to the ready state, as shown in Figure 7-4.The function SwitchToThread provides another way for a thread to yield its processor to another ready thread, if there is one.
The UNIX sleep function is similar to Sleep, but time periods are measured in seconds. To obtain millisecond resolution, use the select or poll functions with no file descriptors. |
