用线程执行类成员函数,高手进来
以下定义的类代码能编译,但结果不对,帮看一下
class CExample
{
private:
static unsigned __stdcall ThreadFunc( void * pArg )
{
CExample * pex = reinterpret_cast<CExample *>(pArg);
return pex->PrivateThreadFunc();
}
DWORD __stdcall PrivateThreadFunc( void )
{
for(int i=0;i<10;i++)
s++;
}
public:
int s;
void StartThread( void )
{
unsigned uThreadID;
HANDLE m_hThread = (HANDLE) _beginthreadex( NULL,0,ThreadFunc,this,0,&uThreadID );
CloseHandle(m_hThread );
}
CExample()
{
s=0;
}
void Show()
{
ShowMessage(s);
}
};