_beginthreadex()与CreateThread()函数的区别

CreateThread()函数是Windows提供的API接口,在C/C++语言另有一个创建线程的函数_beginthreadex(),在很多书上(包括《Windows核心编程》)提到过尽量使用_beginthreadex()来代替使用CreateThread(),这是为什么了?下面就来探索与发现它们的区别吧。

 

       首先要从标准C运行库与多线程的矛盾说起,标准C运行库在1970年被实现了,由于当时没任何一个操作系统提供对多线程的支持。因此编写标准C运行库的程序员根本没考虑多线程程序使用标准C运行库的情况。比如标准C运行库的全局变量errno。很多运行库中的函数在出错时会将错误代号赋值给这个全局变量,这样可以方便调试。但如果有这样的一个代码片段:

if (system("notepad.exe readme.txt") == -1)
{
switch(errno)
{
...//错误处理代码
}
}

假设某个线程A在执行上面的代码,该线程在调用system()之后且尚未调用switch()语句时另外一个线程B启动了,这个线程B也调用了标准C运行库的函数,不幸的是这个函数执行出错了并将错误代号写入全局变量errno中。这样线程A一旦开始执行switch()语句时,它将访问一个被B线程改动了的errno。这种情况必须要加以避免!因为不单单是这一个变量会出问题,其它像strerror()strtok()tmpnam()gmtime()asctime()等函数也会遇到这种由多个线程访问修改导致的数据覆盖问题。

 

为了解决这个问题,Windows操作系统提供了这样的一种解决方案——每个线程都将拥有自己专用的一块内存区域来供标准C运行库中所有有需要的函数使用。而且这块内存区域的创建就是由C/C++运行库函数_beginthreadex()来负责的。下面列出_beginthreadex()函数的源代码(我在这份代码中增加了一些注释)以便读者更好的理解_beginthreadex()函数与CreateThread()函数的区别。


_MCRTIMP uintptr_t __cdecl _beginthreadex(
 void *security,
 unsigned stacksize,
 unsigned (__CLR_OR_STD_CALL * initialcode) (void *),
 void * argument,
 unsigned createflag,
 unsigned *thrdaddr
 )
{
_ptiddata ptd;          //pointer to per-thread data 见注1
uintptr_t thdl;         //thread handle 线程句柄
unsigned long err = 0L; //Return from GetLastError()
unsigned dummyid;    //dummy returned thread ID 线程ID号

// validation section 检查initialcode是否为NULL
_VALIDATE_RETURN(initialcode != NULL, EINVAL, 0);
//Initialize FlsGetValue function pointer
__set_flsgetvalue();
//Allocate and initialize a per-thread data structure for the to-be-created thread.
//相当于new一个_tiddata结构,并赋给_ptiddata指针。
if ( (ptd = (_ptiddata)_calloc_crt(1, sizeof(struct _tiddata))) == NULL )
goto error_return;
// Initialize the per-thread data
//初始化线程的_tiddata块即CRT数据区域 见注2
_initptd(ptd, _getptd()->ptlocinfo);
//设置_tiddata结构中的其它数据,这样这块_tiddata块就与线程联系在一起了。
ptd->_initaddr = (void *) initialcode; //线程函数地址
ptd->_initarg = argument;              //传入的线程参数
ptd->_thandle = (uintptr_t)(-1);
#if defined (_M_CEE) || defined (MRTDLL)
if(!_getdomain(&(ptd->__initDomain))) //见注3
{
goto error_return;
}
#endif  // defined (_M_CEE) || defined (MRTDLL)
// Make sure non-NULL thrdaddr is passed to CreateThread
if ( thrdaddr == NULL )//判断是否需要返回线程ID号
thrdaddr = &dummyid;
// Create the new thread using the parameters supplied by the caller.
//_beginthreadex()最终还是会调用CreateThread()来向系统申请创建线程
if ( (thdl = (uintptr_t)CreateThread(
(LPSECURITY_ATTRIBUTES)security,
stacksize,
_threadstartex,
(LPVOID)ptd,
createflag,
(LPDWORD)thrdaddr))
== (uintptr_t)0 )
{
err = GetLastError();
goto error_return;
}
//Good return
return(thdl); //线程创建成功,返回新线程的句柄.
//Error return
error_return:
//Either ptd is NULL, or it points to the no-longer-necessary block
//calloc-ed for the _tiddata struct which should now be freed up.
//回收由_calloc_crt()申请的_tiddata块
_free_crt(ptd);
// Map the error, if necessary.
// Note: this routine returns 0 for failure, just like the Win32
// API CreateThread, but _beginthread() returns -1 for failure.
//校正错误代号(可以调用GetLastError()得到错误代号)
if ( err != 0L )
_dosmaperr(err);
return( (uintptr_t)0 ); //返回值为NULL的效句柄
}

讲解下部分代码:

1_ptiddataptd;中的_ptiddata是个结构体指针。在mtdll.h文件被定义:

      typedefstruct_tiddata * _ptiddata

微软对它的注释为Structure for each thread's data这是一个非常大的结构体,有很多成员。本文由于篇幅所限就不列出来了。

 

2_initptd(ptd_getptd()->ptlocinfo);微软对这一句代码中的getptd()的说明为:

      

      _ptiddata __cdecl_getptd(void);

_initptd()说明如下:

      

      void__cdecl_initptd(_Inout_ _ptiddata _Ptd,_In_opt_ pthreadlocinfo _Locale);

注释中的CRT C Runtime Library)即标准C运行库。

 

3if(!_getdomain(&(ptd->__initDomain)))中的_getdomain()函数代码可以在thread.c文件中找到,其主要功能是初始化COM环境。

 由上面的源代码可知,_beginthreadex()函数在创建新线程时会分配并初始化一个_tiddata块。这个_tiddata块自然是用来存放一些需要线程独享的数据。事实上新线程运行时会首先将_tiddata块与自己进一步关联起来。然后新线程调用标准C运行库函数如strtok()时就会先取得_tiddata块的地址再将需要保护的数据存入_tiddata块中。这样每个线程就只会访问和修改自己的数据而不会去篡改其它线程的数据了。因此,如果在代码中有使用标准C运行库中的函数时,尽量使用_beginthreadex()来代替CreateThread()相信阅读到这里时,你会对这句简短的话有个非常深刻的印象,如果有面试官问起,你也可以流畅准确的回答了^_^

对于_beginthread函数有如下缺陷

1     创建时不能将线程挂起。 
2     _beginthread产生出来的线程会首先关闭自己的handle,这样做是为了隐藏win32的实现细节,因此_beginthread()传回的参数可能在当时是不可用的。因此,没有这个handle,你就无法等待他结束,无法改变其参数,无法获得结束代码。 


总结:

在Windows下面,比较常见的多线程创建函数是CreateThread(Windows自带的)和_beginthread、_beginthreadex(C运行库的)。

强烈推荐只用_beginthreadex,参数上面与CreateThread基本一样,比CreateThread好的就是,它给每个线程维护一个tiddata数据块,这对于多线程环境非常重要。《Windows核心编程》里也是这么说的。

 

用_beginthreadex还有个好处是,线程结束后,不会自己执行CloseHandle函数(需程序员调用),这样的话,对于WaitFor系列函数的调用就比较方便了。_beginthread在线程结束的时候是会自己CloseHandle。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值