CTestThread.h
public: CTestThread(void); ~CTestThread(void); public: void setvalue(); static DWORD _stdcall SecondThreadFunc(LPVOID lparam); //多线程测试,线程成员函数必须是静态变量,不知道为什么?第二种可以使全局函数
CTestThread.cpp
#include <time.h> #include <windows.h> #include <process.h> using namespace std; CTestThread::CTestThread(void) { tp.aa=14; tp.bb=124.789; } CTestThread::~CTestThread(void) { } DWORD _stdcall CTestThread::SecondThreadFunc(LPVOID lparam) { //WaitForSingleObject(ctrlevent, INFINITE); //等待事件触发 //SetEvent/ResetEvent 事件发信号 与不发信号 /* MYCLASS* pThis = (MYCLASS*)lparam; pThis->DoRecvProc();*/ CTestThread::ThreadParam *pp = ( CTestThread::ThreadParam *)lparam; pp->aa=18; pp->bb=87654.321; _endthreadex(0); return 0; } void CTestThread::setvalue() { //tp.aa=17; //tp.bb=123456.789; HANDLE hThread; unsigned threadID; // hThread=(HANDLE)_beginthreadex(NULL,0,(unsigned(__stdcall *)(void *))SecondThreadFunc,NULL,0,&threadID); hThread= (HANDLE)_beginthreadex(NULL,0,(unsigned(_stdcall *)(void *))SecondThreadFunc,&tp,0,NULL); WaitForSingleObject( hThread, INFINITE ); CloseHandle( hThread ); }