Threads

Windows Mobile 6.5
A version of this page is also available for
4/8/2010

A thread is a path of execution within a process.

Every time the OS creates a process, it also creates at least one thread to give the process as much CPU time as possible.

For example, in many applications, it is useful to create a separate thread to handle printing tasks so that the user can continue to use the application while it is printing.

Each thread shares all resources of the process, including its address space.

Each thread has a stack. The linker, /STACK, sets the stack size for all threads that are created in a process. An individual thread can set its own stack size by calling CreateThread and using the STACK_SIZE_PARAM_IS_A_RESERVATION parameter.

A thread also contains the state of the CPU registers, known as the context, and an entry in the list of the system scheduler.

Use the GetThreadContext function to obtain the thread context and the SetThreadContext function to set the thread context.

Each thread in a process runs independently. Unless you make the threads visible to each other, they are unaware of the other threads in a process.

Threads that share common resources must be synchronized.

An application starts when the system scheduler gives one of its threads control. The system scheduler determines which threads to run and when they should run. Threads of lower priority might need to wait while higher priority threads complete their tasks.

Threads can be in one of the following states:

  • Running
  • Suspended
  • Sleeping
  • Blocked
  • Terminated

When all threads are in the blocked state, Windows Mobile enters idle mode, which stops the CPU from carrying out instructions and consuming power.

From idle mode, you can switch to suspend mode if there is no user activity. Suspend mode can be controlled by the OEM and applications.

To conserve power, use synchronization objects to block threads that are waiting, instead of creating a thread that polls for status, such as the PeekMessage function.

For more information on threads, see the following topics:

Community Additions

Show: