由于类的成员函数在编译时会默认含有本类的this参数,这与线程函数要求的有且仅有一个void*参数不符,因此需要将作为类成员的线程入口函数声明为static,因为static成员函数没有默认的this指针。但static成员函数又不能操作类的非static成员变量,故需要将this指针显示地传给线程函数的void*形参,以此完美地实现线程函数作为类成员。
//VMICMainControl.h
class VMICMainControl
{
private:
static unsigned __stdcall AppRegSetManager(void* lpThis);
public:
void AppRegSetManage();
};
//VMICMainControl.cpp
unsigned __stdcall VMICMainControl::AppRegSetManager(void* lpThis)
{
VMICMainControl* lpSelf = (VMICMainControl*)lpThis;
}
void VMICMainControl::AppRegSetManage()
{
_beginthreadex(NULL,0,&AppRegSetManager,this,0,NULL);
printf("AppRegSetManager线程创建成功!\n\n");
}
本文介绍如何将类成员函数作为线程入口,并通过静态成员函数和传递this指针的方式解决线程函数参数限制的问题。
1111

被折叠的 条评论
为什么被折叠?



