次の方法で共有


CreateThread (Compact 2013)

3/28/2014

This function creates a thread to execute within the address space of the calling process.

Syntax

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES lpsa,
  DWORD cbStack,
  LPTHREAD_START_ROUTINE lpStartAddr,
  LPVOID lpvThreadParam,
  DWORD fdwCreate,
  LPDWORD lpIDThread
);

Parameters

  • lpsa
    [in] Ignored. Must be NULL.
  • cbStack
    [in] Ignored unless the STACK_SIZE_PARAM_IS_A_RESERVATION flag is used. In that case, this parameter specifies the virtual memory reserved for the new thread.

    When ignored, the default stack size for a thread is determined by the linker setting /STACK.

  • lpStartAddr
    [in] Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread; represents the starting address of the thread. For more information on the thread function, see ThreadProc.

    Note

    It is invalid to set lpStartAddr to NULL. If this value is passed in the parameter, the function will return ERROR_INVALID_PARAMETER.

  • lpvThreadParam
    [in] Long pointer to a single 32-bit parameter value passed to the thread.
  • fdwCreate
    [in] Specifies flags that control the creation of the thread.

    The following table shows the values for this parameter.

    Value

    Description

    CREATE_SUSPENDED

    The thread is created in a suspended state and does not run until the ResumeThread function is called.

    The thread can be run immediately after creation if the flag is not specified.

    STACK_SIZE_PARAM_IS_A_RESERVATION

    The cbStack parameter specified the maximum stack size instead of being ignored.

    This is a Windows Embedded Compact only flag for the CreateThread function and is used to reserve the stack size for the thread created.

    By default, a thread is created with 64 KB stack reserved. You can use this flag to increase the stack size for the new thread that was created.

  • lpIDThread
    [out] Long pointer to a 32-bit variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.

Return Value

A handle to the new thread indicates success. NULL indicates failure. To get extended error information, call GetLastError.

Remarks

The number of threads a process can create is limited by the available virtual memory and depends on the default stack size.

If every thread has 1 MB of stack space, you can create a maximum of 32 threads.

If you reduce the default stack size, you can create more threads.

Your application will perform better if you create one thread per process, and build queues of requests for which the application maintains the context information. That is because a thread processes all requests in a queue before processing requests in the next queue.

The new thread handle is created with THREAD_ALL_ACCESS to the new thread. The handle can be used in any function that requires a thread object handle.

Thread execution begins at the function specified by the lpStartAddr parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.

If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing .dll files.

The thread is created with a thread priority of THREAD_PRIORITY_NORMAL.

Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.

When a thread terminates, the thread object attains a signaled state, satisfying threads that were waiting on the object.

The thread object remains in the system until the thread has terminated and all handles to it are closed through a call to CloseHandle.

The ExitThread function, CreateThread function, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time.

The following list shows the restrictions during the process:

  • During process startup and DLL initialization routines, threads can be created, but they do not begin execution until DLL initialization is done for the process.
  • In a process, only one thread at a time can be in a DLL initialization or detach routine.

Requirements

Header

winbase.h

Library

coredll.lib

See Also

Reference

Process and Thread Functions
CreateProcess
ExitThread
GetExitCodeThread
GetThreadPriority
ResumeThread
SetThreadPriority
ThreadProc

Other Resources

CloseHandle