Thread Priorities

< BACK  NEXT >
[oR]

The previous section described how threads are scheduled for execution using the quantum time period. The thread scheduler uses a round-robin algorithm for scheduling threads. However, this ignores the fact that threads can have different priorities, and this affects how frequently a thread is scheduled.

In Windows CE 3.0 a thread can be assigned any one of 255 different priorities, with 0 being the highest priority and 255 the lowest. The seven lowest priorities (255 to 249) are application thread priorities, while the remainder are real-time priorities. In Windows NT/98/2000 a thread has a priority relative to the process's priority class. Windows CE does not use priority classes, and each thread has a priority in its own right.

The scheduler first schedules threads at the highest priority level in a round-robin manner. Only when all threads at the highest level have blocked does the scheduler then schedule threads at the next highest level. This process is repeated down all the different priority levels. If, while a lower-priority thread is executing, a higher-level thread unblocks, the lower-priority thread is stopped executing (even if it has not finished its quantum), and the higher-priority thread is scheduled. A real time priority thread cannot be preempted (that is, swapped out) except by an interrupt-service routine even if its time quantum period has elapsed.

All threads are initially created at the "normal" priority. The application then changes the thread's priority appropriately. A thread's priority can be set by either calling SetThreadPriority (to set an application priority) or CeSetThreadPriority (to set an application or real-time priority). The SetThreadPriority function is passed the handle to a thread and a constant specifying which priority to use (Table 5.7). Note that THREAD_PRIORITY_TIME_CRITICAL is a real-time priority. The constants in Table 5.7 have the values 0 for THREAD_PRIORITY_TIME_CRITICAL to 7 for THREAD_PRIORITY_TIME_CRITICAL. You can see that these constants do not map to the priority values of 0 to 255 used by CeSetThreadPriority. For this reason, you should only use these constants with SetThreadPriority.

There is only one situation where a thread's priority is automatically changed by the operating system, and that is priority inversion. If a low-priority thread is using a resource that a high-priority thread is waiting on, the operating system temporarily boosts the lower-priority thread until it releases the resource required by the higher-priority thread. Unlike Windows NT/98/2000, Windows CE does not provide "foreground boosting" whereby the application in the foreground has its thread priorities set to a value greater than other applications.

Table 5.7. CeSetThreadPriority priority constants
Constant Purpose
THREAD_PRIORITY_TIME_CRITICAL Indicates 3 points above normal priority
THREAD_PRIORITY_HIGHEST Indicates 2 points above normal priority
THREAD_PRIORITY_ABOVE_NORMAL Indicates 1 point above normal priority
THREAD_PRIORITY_NORMAL Indicates normal priority
THREAD_PRIORITY_BELOW_NORMAL Indicates 1 point below normal priority
THREAD_PRIORITY_LOWEST Indicates 2 points below normal priority
THREAD_PRIORITY_ABOVE_IDLE Indicates 3 points below normal priority
THREAD_PRIORITY_IDLE Indicates 4 points below normal priority

In Listing 5.11 the code obtains the current thread priority by calling CeGetThreadPriority. This will generally return the value "251". This corresponds to THREAD_PRIORITY_NORMAL in Table 5.7. A call is then made to CeSetThreadPriority to set the thread's priority to 140. This is a real time priority, so subsequent code will be executed without interruption. After displaying the new thread priority, the thread's priority is set back to its original value.

Listing 5.11 Sets real time thread priorities
 void Listing5_11() {   int nPri = CeGetThreadPriority(GetCurrentThread());   cout   _T("Pri: ")   nPri   endl;   CeSetThreadPriority(GetCurrentThread(), 140);   cout   _T("New Pri: ")          CeGetThreadPriority(GetCurrentThread())          endl;   CeSetThreadPriority(GetCurrentThread(), nPri); } 

You should take care when using real time priorities. An application can easily take over the processor and not let other applications, or essential parts of the operating system, run correctly. If you do need to create real time threads, ensure that they remain real time for the minimum required time or remain blocked for the majority of time.


< 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