_beginthread调用CreateThread, 看看下面vc里面的源码就知道了unsigned long __cdecl _beginthread (
void (__cdecl * initialcode) (void *),
unsigned stacksize,
void * argument
)
{
_ptiddata ptd; /* pointer to per-thread data */
unsigned long thdl; /* thread handle */
unsigned long errcode = 0L; /* Return from GetLastError() */ /*
* Allocate and initialize a per-thread data structure for the to-
* be-created thread.
*/
if ( (ptd = _calloc_crt(1, sizeof(struct _tiddata))) == NULL )
goto error_return; /*
* Initialize the per-thread data
*/ _initptd(ptd); ptd->_initaddr = (void *) initialcode;
ptd->_initarg = argument; /*
* Create the new thread. Bring it up in a suspended state so that
* the _thandle and _tid fields are filled in before execution
* starts.
*/
if ( (ptd->_thandle = thdl = (unsigned long)
CreateThread( NULL,
stacksize,
_threadstart,
(LPVOID)ptd,
CREATE_SUSPENDED,
(LPDWORD)&(ptd->_tid) ))
== 0L )
{
errcode = GetLastError();
goto error_return;
} /*
* Start the new thread executing
*/
if ( ResumeThread( (HANDLE)thdl ) == (DWORD)(-1L) ) {
errcode = 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.
*/
_free_crt(ptd); /*
* Map the error, if necessary.
*/
if ( errcode != 0L )
_dosmaperr(errcode); return((unsigned long)-1L);
}
void (__cdecl * initialcode) (void *),
unsigned stacksize,
void * argument
)
{
_ptiddata ptd; /* pointer to per-thread data */
unsigned long thdl; /* thread handle */
unsigned long errcode = 0L; /* Return from GetLastError() */ /*
* Allocate and initialize a per-thread data structure for the to-
* be-created thread.
*/
if ( (ptd = _calloc_crt(1, sizeof(struct _tiddata))) == NULL )
goto error_return; /*
* Initialize the per-thread data
*/ _initptd(ptd); ptd->_initaddr = (void *) initialcode;
ptd->_initarg = argument; /*
* Create the new thread. Bring it up in a suspended state so that
* the _thandle and _tid fields are filled in before execution
* starts.
*/
if ( (ptd->_thandle = thdl = (unsigned long)
CreateThread( NULL,
stacksize,
_threadstart,
(LPVOID)ptd,
CREATE_SUSPENDED,
(LPDWORD)&(ptd->_tid) ))
== 0L )
{
errcode = GetLastError();
goto error_return;
} /*
* Start the new thread executing
*/
if ( ResumeThread( (HANDLE)thdl ) == (DWORD)(-1L) ) {
errcode = 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.
*/
_free_crt(ptd); /*
* Map the error, if necessary.
*/
if ( errcode != 0L )
_dosmaperr(errcode); return((unsigned long)-1L);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货