Thread Scheduling

< BACK  NEXT >
[oR]

The Windows CE thread scheduler is responsible for ensuring that threads get the proper amount of time to execute their code. Each thread is given a "quantum" of time in which to execute code. Once the quantum of time has elapsed, the thread scheduler allows another thread to execute for its quantum. In Windows CE versions prior to 3.0, the quantum was set at 25 milliseconds. In Windows CE 3.0 it is set to 100 milliseconds (although this figure can be changed by an OEM). This means that in Windows CE 3.0 a thread can execute for up to 100 milliseconds without interruption.

A thread can change its quantum to make it longer or shorter. The code in Listing 5.10 displays the current quantum time for the thread using CeGetThreadQuantum. This function is passed the thread's handle that is obtained through calling GetCurrentThread. The CeSetThreadQuantum function is then called to set the quantum time for the current thread to 20 milliseconds.

Listing 5.10 Thread quantums
 void Listing5_10() {   cout   CeGetThreadQuantum(GetCurrentThread())   endl;   CeSetThreadQuantum(GetCurrentThread(), 20); } 

The function GetCurrentThread used in Listing 5.10 returns a pseudohandle for the current thread. This handle does not have to be closed through a call to CloseHandle since it is a pseudohandle.

You might want to increase the thread quantum time if, for example, you are sending data to an instrument that needs to receive the data without interruption. You would set the quantum time to the period of time you expected the transmission to take. Then, once the transmission is complete, you would call return the quantum value back to its previous value, and then call Sleep(0) to end your quantum. Of course, you do not want to set the quantum period to be too long, otherwise operating system and other processes won't get an opportunity to execute. Finally, thread scheduling is dependent on the thread's priority, and this is discussed in the next section.

When the scheduler swaps out a thread, it saves a "thread context". This context contains the current state of the processor (including all the registers, program counter, stack frames, and so on). Then, when the thread is to receive its next quantum, the thread context is restored back into the processor and the thread set to running again. A thread can get another thread's context by calling the GetThreadContext function, passing the handle to the thread and a CONTEXT structure pointer. This is typically a debugging operation. The function SetThreadContext allows the current context for a thread to be changed. The modified context will be used the next time the thread is scheduled for a quantum. The CONTEXT structure is highly dependent on the device's processor, since it contains members for CPU registers and so on. It is declared in the header file winnt.h.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net