C/C++中CreateThread参数的使用方法
项目是VS2003下的,用到了CreateThread函数,看到网上说不应该调用CreateThread,而应该使用_beginthreadex。因为刚好用到了,就还是写一下CreateThread中参数的使用方法。
见代码:
先定义要传递的参数结构体:
typedef struct SParam
{
int No;
unsigned short chnlID;
unsigned short sessionID;
}uParam,*sParam;
DWORD WINAPI AccountManager(PVOID pParam);
void main()
{
DWORD dwThreadId;
HANDLE hThrd = NULL; // thread handle
SParam sparam;
SParam *p;
sparam.No = 1;
sparam.chnlID = 1;
sparam.sessionID = 1;
p = &sparam;
hThrd = (HANDLE)CreateThread(NULL,
0,
AccountManager,
p,
0,
dwThreadId;
}
DWORD WINAPI AccountManager(PVOID pParam)
{
sParam sparam;
sparam = (sParam)pParam;
try
{
/*Run为自己写的一个方法,Run(int i,unsigned short chnlID,unsigned short sessionID)*/
Run(sparam->No,sparam->chnlID,sparam->sessionID);
}
catch (...)
{
logger.error("AccountManager(%d): System error.\r\n", threadId);
}
}
大体意思是这样,代码仅供参考。
见代码:
先定义要传递的参数结构体:
typedef struct SParam
{
int No;
unsigned short chnlID;
unsigned short sessionID;
}uParam,*sParam;
DWORD WINAPI AccountManager(PVOID pParam);
void main()
{
DWORD dwThreadId;
HANDLE hThrd = NULL; // thread handle
SParam sparam;
SParam *p;
sparam.No = 1;
sparam.chnlID = 1;
sparam.sessionID = 1;
p = &sparam;
hThrd = (HANDLE)CreateThread(NULL,
0,
AccountManager,
p,
0,
dwThreadId;
}
DWORD WINAPI AccountManager(PVOID pParam)
{
sParam sparam;
sparam = (sParam)pParam;
try
{
/*Run为自己写的一个方法,Run(int i,unsigned short chnlID,unsigned short sessionID)*/
Run(sparam->No,sparam->chnlID,sparam->sessionID);
}
catch (...)
{
logger.error("AccountManager(%d): System error.\r\n", threadId);
}
}
大体意思是这样,代码仅供参考。