解决类内部使用多线程问题

额...解决办法貌似很简单,,,,只需把线程过程声明为静态就行了 static 

#include <windows.h>
#include <IOSTREAM>

struct myStruct{
	int a;
	char b;
};

class MyTestClass{
public:
	
	BOOL MycreateThread(){
		myStruct *pStruct=new myStruct;
		pStruct->a=123;
		pStruct->b='f';
		m_hThread=CreateThread(NULL,0,MyThreadProc,(LPVOID)pStruct,0,NULL);
		return m_hThread?TRUE:FALSE;
	};
	
	HANDLE getThreadHandle(){return m_hThread;}

	static	DWORD WINAPI MyThreadProc(LPVOID lpParamete){
		myStruct *pStruct=((myStruct*)lpParamete);
		
		std::cout<<"Thread is Running...."<<std::endl
			<<"a="<<pStruct->a<<"\t b="<<pStruct->b<<std::endl;
		return 0;
	}

	~MyTestClass(){CloseHandle(m_hThread);}
private:
	HANDLE m_hThread;
};

int main()
{
	MyTestClass myTestClass;
	myTestClass.MycreateThread();
	WaitForSingleObject(myTestClass.getThreadHandle(),INFINITE);
	return 0;
}



评论
文章信息
作者: admin
目录: c
创建: 2014-02-27
更新: 2014-02-27
如果文章对你有帮助,请博主喝杯咖啡:-)