C++类中封装线程函数

线程函数必须声明为静态

非静态函数都要有一个隐含参数,作为this指针
而线程函数只能是普通函数,要写在类里面一定要是静态的,然后把类指针作为参数进行显式传递

class MyClass
{
public:
	void zz()
	{
		i = 1000;
		CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GO, this, 0, NULL);
	}

private:
	int i = 0;

	VOID WINAPI GO(LPVOID lp)
	{
		MyClass* m = (MyClass*)lp;
		cout << m->i << endl;
	}

};

int main()
{
	MyClass* m = new MyClass;
	m->zz();

	cin.get();
}

猜你喜欢

转载自blog.csdn.net/weixin_42052102/article/details/82872862