Creating and Terminating a Thread

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

To create a thread, call the CreateThread function.

The following code example shows the CreateThread function prototype:

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES  lpThreadAttributes,
  DWORD  dwStackSize,
  LPTHREAD_START_ROUTINE  lpStartAddress,
  LPVOID  lpParameter,
  DWORD  dwCreationFlags,
  LPDWORD  lpThreadId
);

Because Windows Mobile does not support the lpThreadAttributes and dwStackSize parameters, you must set them to NULL or zero.

The following table describes the available CreateThread parameters.

Parameter Description

lpStartAddress

Points to the start of the thread routine.

lpParameter

Specifies an application-defined value that is passed to the thread routine.

dwCreationFlags

Set to zero or to CREATE_SUSPENDED and/or STACK_SIZE_PARAM_IS_A_RESERVATION.

lpThreadId

Points to a DWORD that receives the new thread's identifier.

If CreateThread is successful, it returns the handle to the new thread and the thread identifier.

You can also retrieve the thread identifier by calling the GetCurrentThreadId function from within the thread. In Windows Mobile, the value returned in GetCurrentThreadId is the actual thread handle.

You can also retrieve a handle to a thread by calling the GetCurrentThread function.This function returns a pseudo-handle to the thread that is valid only while in the thread.

If you specify CREATE_SUSPENDED in the dwCreationFlags parameter, the thread is created in a suspended state and must be resumed with a call to the ResumeThread function.

You can terminate a thread by calling ExitThread, which frees the resources that are used by a thread when they are no longer needed. Calling ExitThread for the primary thread of an application causes the application to close.

Community Additions

Show: