CreateThread

CreateThread

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

HANDLE CreateThread(
LPSECURITY_ATTRIBUTES , 
DWORD , 
LPTHREAD_START_ROUTINE , 
LPVOID , 
DWORD , 
LPDWORD ); 

Parameters

lpThreadAttributes
Ignored. Must be NULL.
dwStackSize
Ignored. The default stack size for a thread is determined by the linker setting /STACK.
lpStartAddress
Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc.
lpParameter
Long pointer to a single 32-bit parameter value passed to the thread.
dwCreationFlags
Specifies flags that control the creation of the thread.

Value Description
CREATE_SUSPENDED The thread is created in a suspended state, and will not run until the ResumeThread function is called.
0 The thread runs immediately after creation.

lpThreadId
Long pointer to a 32-bit variable that receives the thread identifier.

If this parameter is NULL, the thread identifier is not returned.

Return Values

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. The number of threads that can be created depends on the default stack size. If every thread has one megabyte of stack space., you can create at most 32 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process 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.

The thread execution begins at the function specified by the lpStartAddress 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.

The CreateThread function may succeed even if lpStartAddress points to data, code, or is not accessible. 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 dynamic-link libraries (DLLs).

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 any threads that were waiting on the object.

The thread object remains in the system until the thread has terminated and all handles to it have been 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. This means that the following restrictions hold:

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

你可能感兴趣的:(C++)