Start address of routine that begins execution of new thread.
stack_size
Stack sizefor new thread or 0.
arglist
Argument list to be passed to new thread or NULL.
security
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes.If NULL, the handle cannot be inherited. Must be NULL for Windows 95 applications.
initflag
Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended); use ResumeThread to execute the thread.
thrdaddr
Points to a 32-bit variable that receives the thread identifier.
Return Value
If successful, each of these functions returns a handle to the newly created thread. _beginthread returns –1L on an error,in which case errno is set to EAGAIN if there are too many threads,or to EINVAL if the argument is invalid or the stack size is incorrect. _beginthreadex returns 0 on an error,in which case errno and _doserrno are set.(成功的话,返回对应的线程句柄。_beginthread失败返回-1,EAGAIN表示当前线程太多而无法创建,EINVAL表示参数错误。_beginthreadex 失败返回0)
See Standard Types for more information on uintptr_t.
See _doserrno, errno, _sys_errlist,and _sys_nerr for more information on these and other return codes.
Remarks
The _beginthread function creates a thread that begins execution of a routine at start_address. The routine at start_address must use the __cdecl calling convention and should have no return value. When the thread returns from that routine, it is terminated automatically.For more information on threads, see Multithreading.
_beginthreadex resembles the Win32 CreateThread API more closely than _beginthread does. _beginthreadex differs from _beginthread in the following ways:
_beginthreadex has three additional parameters: initflag, security, threadaddr. The new thread can be created in a suspended state, with a specified security (Windows NT only),and can be accessed using thrdaddr, which is the thread identifier.
The routine at start_address passed to _beginthreadex must use the __stdcall calling convention and must return a thread exit code.
_beginthreadex returns 0 on failure, rather than –1L.
A thread created with _beginthreadex is terminated by a call to _endthreadex.
The _beginthreadex function gives you more control over how the thread is created than _beginthread does. The _endthreadex function is also more flexible.For example, with _beginthreadex, you can use security information, set the initial state of the thread (running or suspended),and get the thread identifier of the newly created thread. You are also able to use the thread handle returned by _beginthreadex with the synchronization APIs, which you cannot do with _beginthread.
It is safer to use _beginthreadex than _beginthread.If the thread spawned by _beginthread exits quickly, the handle returned to the caller of _beginthread may be invalid or, worse, point to another thread. However, the handle returned by _beginthreadex has to be closed by the caller of _beginthreadex, so it is guaranteed to be a valid handle if _beginthreadex did not return an error.
You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.
_endthread automatically closes the thread handle (whereas _endthreadex does not). Therefore, when using _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32 CloseHandle API.This behavior differs from the Win32 ExitThread API.
Note For an executable file linked with LIBCMT.LIB, do notcall the Win32 ExitThread API;this prevents the run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then callExitThread.
The operating system handles the allocation of the stack when either _beginthread or _beginthreadex is called; you do not need to pass the address of the thread stack to either of these functions.In addition, the stack_size argument can be 0,in which case the operating system uses the same value as the stack specified for the main thread.
arglist is a parameter to be passed to the newly created thread. Typically it is the address of a data item, such as a character string. arglist may be NULL if it is not needed, but _beginthread and _beginthreadex must be provided with some value to pass to the new thread. All threads are terminated if any thread calls abort, exit, _exit,orExitProcess.