//相当于new一个_tiddata结构,并赋给_ptiddata指针。
if((ptd=(_ptiddata)_calloc_crt(1,sizeof(struct_tiddata)))==NULL)
gotoerror_return;
//Initializetheper-threaddata
//初始化线程的_tiddata块即CRT数据区域见注2
_initptd(ptd,_getptd()->ptlocinfo);
//设置_tiddata结构中的其它数据,这样这块_tiddata块就与线程联系在一起了。
ptd->_initaddr=(void*)initialcode;//线程函数地址
ptd->_initarg=argument;//传入的线程参数
ptd->_thandle=(uintptr_t)(-1);
#ifdefined(_M_CEE)||defined(MRTDLL)
if(!_getdomain(&(ptd->__initDomain)))//见注3
{
gotoerror_return;
}
#endif//defined(_M_CEE)||defined(MRTDLL)
//Makesurenon-NULLthrdaddrispassedtoCreateThread
if(thrdaddr==NULL)//判断是否需要返回线程ID号
thrdaddr=&dummyid;
//Createthenewthreadusingtheparameterssuppliedbythecaller.
//_beginthreadex()最终还是会调用CreateThread()来向系统申请创建线程
if((thdl=(uintptr_t)CreateThread(
(LPSECURITY_ATTRIBUTES)security,
stacksize,
_threadstartex,
(LPVOID)ptd,
createflag,
(LPDWORD)thrdaddr))
==(uintptr_t)0)
{
err=GetLastError();
gotoerror_return;
}
//Goodreturn
return(thdl);//线程创建成功,返回新线程的句柄.
//Errorreturn
error_return:
//EitherptdisNULL,oritpointstotheno-longer-necessaryblock
//calloc-edforthe_tiddatastructwhichshouldnowbefreedup.
//回收由_calloc_crt()申请的_tiddata块
_free_crt(ptd);
//Maptheerror,ifnecessary.
//Note:thisroutinereturns0foilure,justliketheWin32
//APICreateThread,but_beginthread()returns-1foilure.
//校正错误代号(可以调用GetLastError()得到错误代号)
if(err!=0L)
_dosmaperr(err);
return((uintptr_t)0);//返回值为NULL的效句柄
}
讲解下部分代码:
注1._ptiddataptd;中的_ptiddata是个结构体指针。在mtdll.h文件被定义:
typedefstruct_tiddata*_ptiddata
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33747-4.html
亏了再圈
20年不用修