http://blog.csdn.net/cjcy1984001/article/details/6675669
线程开始和停止函数!
unsigned long _beginthreadex( void *security, unsigned stack_size,
unsigned ( __stdcall *start_address )( void * ),
void *arglist, unsigned initflag, unsigned *thrdaddr );
第一个参数是线程的安全属性,如果为NULL则为默认安全属性
第二个参数是用来指定线程堆栈的大小,如果为0,则线程堆栈大小和
创建他的线程的相同。一般用0
第三个参数指定线程函数,也就是线程调用执行的函数地址
第四个参数为传递给线程的参数列表 如果多于一个的话,使用结构,然
然后传给结构的指针
最后一个是线程的标识ID
void _endthreadex(unsigned status);
停止线程返回status中指定的代码
例子:
struct agrclist{
char *data;
int count;
};
unsigned __stdcall DoTest(void *mArgclist){
agrclist *pagrclist;
pagrclist=(struct agrclist *)mArgclist;
cout << pagrclist->data;
cout << pagrclist->count;
_endthreadex(0);
return 0;
}
void main(void){
char buf[128];
HANDLE hThread;
unsigned int threadid;
agrclist magrclist,*pmagrclist;
pmagrclist=&magrclist;
pmagrclist->data=buf;
pmagrclist->count=1;
hThread=(HANDLE)_beginthreadex(NULL,0,&DoTest,pmagrclist,NULL,&threadid);
closeHandle(hThread);
http://blog.csdn.net/kailan818/article/details/5907908
小光和Kimi建议创建线程应该用_beginThreadex,ripple里面就是用的这个。
花了老半天才把createThread改成_beginThreadex,例子如下:
//sipvoiplink.h
class SIPVoIPLink
{
private:
static unsigned __stdcall ReceivingThrd(void * pParam);
}
//sipvoiplink.cpp
#include <Winsock2.h>
#include <process.h>
bool SIPVoIPLink::init()
{
......
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, &SIPVoIPLink::ReceivingThrd, (LPVOID)this, 0, &threadID );
if(hThread == NULL)
return false;
}
unsigned __stdcall SIPVoIPLink::ReceivingThrd(void * pParam)
{
//getEvent();
((SIPVoIPLink *)pParam)->getEvent();
return 0;
}
unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsignedinitflag, unsigned *thrdaddr );
//第1个参数:安全属性,NULL为默认安全属性
//第2个参数:指定线程堆栈的大小。如果为0,则线程堆栈大小和创建它的线程的相同。一般用0
//第3个参数:指定线程函数的地址,也就是线程调用执行的函数地址(用函数名称即可,函数名称就表示地址)
//第4个参数:传递给线程的参数的指针,可以通过传入对象的指针,在线程函数中再转化为对应类的指针
//第5个参数:线程初始状态,0:立即运行;CREATE_SUSPEND:suspended(悬挂)
//第6个参数:用于记录线程ID的地址
一个线程的好例子:http://www.cppblog.com/mzty/archive/2007/07/25/28756.html
// In C++ you must employ a free (C) function or a static
// class member function as the thread entry-point-function.
// Furthermore, _beginthreadex() demands that the thread
// entry function signature take a single (void*) and returned
// an unsigned.