如何TestFunction2
使用TestFunction1
拨打ExecuteLocalThread
代替beginthreadex
,因为TestFunction2
包含3个附加参数
unsigned __stdcall TestFunction1(void* timerPointer)
{
unsigned result =0;
printf("thread is running\n");
return result;
}
unsigned __stdcall TestFunction2(void* timerPointer, wchar_t *appId, wchar_t *userName, wchar_t *password)
{
unsigned result =0;
printf("thread is running\n");
return result;
}
void ExecuteLocalThread()
{
HANDLE heartBeatThread;
unsigned int hbThreadID;
heartBeatThread = (HANDLE)_beginthreadex(NULL, 0 , &TestFunction1, (void*)this, CREATE_SUSPENDED, &hbThreadID);
ResumeThread( heartBeatThread );
}
答案 0 :(得分:8)
使用C ++ 11线程:
std::thread thr(func, arg0, arg1, arg2);
答案 1 :(得分:3)
创建一个包含您要传递的所有数据的结构,然后将指针传递给您的线程入口点
typedef struct MyThreadData {
void* timerPointer;
wchar_t *appId;
wchar_t *userName;
wchar_t *password;
}; MyThreadData
unsigned int hbThreadID;
void* threadData = calloc(1, sizeof(*threadData));
/* populate threadData */
heartBeatThread = (HANDLE)_beginthreadex(NULL, 0,
&TestFunction1, threadData,
CREATE_SUSPENDED, &hbThreadID);
unsigned __stdcall TestFunction1(void* ptr) {
MyThreadData* threadData = (MyThreadData*)ptr;
unsigned result;
printf("thread is running\n");
result = TestFunction2(ptr->timerPointer, ptr->appId, ptr->userName, ptr->password);
free(threadData);
return result;
}
unsigned __stdcall TestFunction2(void* timerPointer, wchar_t *appId, wchar_t *userName, wchar_t *password) {
unsigned result =0;
return result;
}
答案 2 :(得分:0)
按建议创建结构。如果代码在一个类中并且该类已经包含该信息,那么您也可以使用它:
unsigned __stdcall TestFunctionProxy(void* p)
{
assert(p);
MyClass* c = static_cast<MyClass*>(p);
c->TestFunction();
return 0;
}
如果这不起作用,因为TestFunction()是私有的,请将其设为友元函数。顺便说一句:在悬挂状态下启动线程然后释放它是不必要的。直接启动线程。
最后,请记住,使用适当的C ++线程库,例如: Boost.Thread会使这更灵活,并为您的问题提供如何传递更多参数的解决方案。