首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++线程函数参数传递的有关问题

2013-08-01 
c++线程函数参数传递的问题test.cpp中,在一个类成员函数中启动线程:int CSingleUserMiner::start_threads(

c++线程函数参数传递的问题
test.cpp中,在一个类成员函数中启动线程:
int CSingleUserMiner::start_threads(string start_time,string end_time,string id,int type,string flag)
{
...
CreateThread(NULL, 0, CSUMiner::createHandleThread, this, 0, NULL);
...
}

test.h中:
线程函数
static DWORD WINAPI createHandleThread(LPVOID lpParam)
{
CSUMiner* pThis = (CSUMiner *)lpParam;
return pThis->get_data();
}

DWORD get_data(string tablename,string id,int type);

现在遇到的问题是,不知道如何将线程函数需要的这几个参数传过去?
如何实现?谢谢
[解决办法]
将这些参数组成一个结构体类型,createHandleThread里面把lpParam转换成指向结构体的指针,然后访问结构体的成员,就是你需要的各个参数。
[解决办法]
typedef tagMyParam
{
string start_time;
string end_time;
string id;
int type;
string flag;
CSingleUserMiner * pCaller;
}MYPARAM;
MYPARAM m_threadParam;
int CSingleUserMiner::start_threads(string start_time,string end_time,string id,int type,string flag)
{
...
threadParam.start_time = start_time;
threadParam.end_time = end_time;
threadParam.id = id;
threadParam.type = type;
threadParam.flag = flag;
threadParam.pCaller = this;
CreateThread(NULL, 0, CSUMiner::createHandleThread, &MYPARAM, 0, NULL);
...
}

static DWORD WINAPI createHandleThread(LPVOID lpParam)
{
MYPARAM* pParam= (MYPARAM*)lpParam;
CSUMiner* pThis = pParam->pCaller;
....// other params
return pThis->get_data();
}

[解决办法]
结构体 或者 全局变量

热点排行