1,为什么建议使用_beginthread和_beginthreadex:
A thread in an executable that calls the C run-time library (CRT)should use the _beginthread and _endthread functions for threadmanagement rather than CreateThread and ExitThread; this requiresthe use of the multi-threaded version of the CRT. It is safe to call CreateThreadand ExitThread from a thread in a DLL that links to the static CRT aslong as the thread does not call the DisableThreadLibraryCalls function.
也就是说:CreateThread创建的线程,使用的不是多线程版本CRT;而_beginthread使用的是多线程版本。
2,_beginthread和_beginthreadex差异点
| _beginthread | _beginthreadex |
原型 | uintptr_t _beginthread( void( *start_address )( void * ), unsigned stack_size, void *arglist ); | uintptr_t _beginthreadex( void *security, unsigned stack_size, unsigned ( *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr ); |
start_address | The routine at start_address must use the __cdecl calling convention and should have no return value | The routine at start_address must use the __stdcall calling convention and must return a thread exit code. |
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. | |
if the newly created thread exits too quickly | the handle returned to the caller of _beginthread might be invalid or, worse, point to another thread | 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. |
terminate a thread | 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. | |
_endthread automatically closes the thread handle | _endthreadex does not closes the thread handle | |
ohter diffrence | 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. |