After this lesson, you will be able to:
■ Create and stop a thread.
■ Manage thread priorities.
■ Synchronize multiple threads.
■ Debug thread synchronization issues.
Estimated lesson time: 45 minutes.
Processes and Threads
A process is a single instance of an application. It has a processing context, which can include a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, and environment variables. It also has a primary thread of execution. A thread is the basic unit of execution managed by the scheduler. In a Windows process, a thread can create additional threads. There is no hard-coded maximum number of threads per process. The maximum number depends on available memory resources because every thread uses memory and the physical memory is limited on the platform. The maximum number of processes on Windows Embedded CE is limited to 32,000.
Thread Scheduling on Windows Embedded CE
Windows Embedded CE supports preemptive multitasking to run multiple threads from various processes simultaneously. Windows Embedded CE performs thread scheduling based on priority. Each thread on the system has a priority ranging from zero to 255. Priority zero is the highest priority. The scheduler maintains a priority list and selects the thread to run next according to the thread priority in a round-robin fashion. Threads of the same priority run sequentially in a random order. It is important to note that thread scheduling relies on a time-slice algorithm. Each thread can only run for a limited amount of time. The maximum possible time slice that a thread can run is called the quantum. Once the quantum has elapsed, the scheduler suspends the thread and resumes the next thread in the list.
Applications can set the quantum on a thread-by-thread basis to adapt thread scheduling according to application needs. However, changing the quantum for a thread does not affect threads with a higher priority because the schedule selects threads with higher priority to run first. The scheduler even suspends lower-priority threads within their time slice if a higher-priority thread becomes available to run.
Process Management API
Windows Embedded CE includes several process management functions as part of the core Win32 API. Three important functions are listed in Table 3-8 that are useful for creating and ending processes.
Table 3-8 Process management functions
Function | Description |
---|---|
CreateProcess | Starts a new process. |
ExitProcess | Ends a process with cleanup and unloading DLLs. |
TerminateProcess | Terminates a process without cleanup or unloading DLLs. |
For more information about process management functions and complete API documentation, see the Core OS Reference for Windows Mobile® 6 and Windows Embedded CE 6.0, available on the Microsoft MSDN website at http://msdn2.microsoft.com/en-us/library/aa910709.aspx.
Thread Management API
Each process has at least one thread called the primary thread. This is the main thread of the process, which means that exiting or terminating this thread also ends the process. The primary thread can also create additional threads, such as worker threads, to perform parallel calculations or accomplish other processing tasks. These additional threads can create more threads if necessary by using the core Win32 API. Table 3-9 lists the most important functions to use in applications that work with threads on Windows Embedded CE.
Table 3-9 Thread management functions
Function | Description |
---|---|
CreateThread | Creates a new thread. |
ExitThread | Ends a thread. |
TerminateThread | Stops a specified thread without running cleanup or other code. Use this function only in extreme cases because terminating a thread can leave memory objects behind and cause memory leaks. |
GetExitCodeThread | Returns the thread exit code. |
CeSetThreadPriority | Sets the thread priority. |
CeGetThreadPriority | Gets the current thread priority. |
SuspendThread | Suspends a thread. |
ResumeThread | Resumes a suspended thread. |
Sleep | Suspends a thread for a specified amount of time. |
SleepTillTick | Suspends a thread until the next system tick. |
For more information about thread management functions and complete API documentation, see the Core OS Reference for Windows Mobile 6 and Windows Embedded CE 6.0, available on the Microsoft MSDN website at http://msdn2.microsoft.com/en-us/library/aa910709.aspx.
Creating, Exiting, and Terminating Threads
The CreateThread function used to create a new thread expects several parameters that control how the system creates the thread and the instructions that the thread runs. Although it is possible to set most of these parameters to null or zero, it is necessary to provide at least a pointer to an application-defined function that the thread is supposed to execute. This function typically defines the core processing instructions for the thread, although you can also call other functions from within this function. It is important to pass the core function as a static reference to CreateThread because the linker must be able to determine the core function's starting address at compile time. Passing a non-static function pointer does not work.