为什么建议使用_beginthread和_beginthreadex以及_beginthread和_beginthreadex差异点

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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值