Scheduling a Thread

Windows CE uses a priority-based time-slice algorithm to schedule the execution of threads. Because Windows CE does not have priority classes, the process in which the thread runs does not influence thread priorities. All the priorities can be used in the same process. A thread can have one of the eight priorities. The following table describes these priorities.

Priority
Description
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

Threads with a higher priority run first. Threads with the same priority run in a round-robin fashion—when a thread has stopped running, all other threads of the same priority run before the original thread can continue. Threads at a lower priority do not run until all threads with a higher priority have either finished or have been blocked. If one thread is running and a thread of higher priority is unblocked, the lower-priority thread is immediately suspended and the higher-priority thread is scheduled.

Threads run for a specific slice of time—called a quantum—which has a default value of 25 milliseconds. An OEM can specify a different quantum. If, after the quantum has elapsed, the thread has not relinquished its time slice and is not time-critical, it is suspended and another thread is scheduled to run. Threads having a priority level of THREAD_PRIORITY_TIME_CRITICAL cannot be preempted except by an interrupt service routine (ISR).

For the most part, thread priorities are fixed and do not change. However, there is one exception, called priority inversion. If a low-priority thread is using a resource that a high-priority thread is waiting to use, the kernel temporarily boosts the priority of the low-priority thread until it releases the resource that is required by the higher-priority thread.

To query the priority level of a thread, call the GetThreadPriority function. To change the priority level of a thread, call the SetThreadPriority function.