thread 함수를 클래스의 멤버 함수로 지정하고 싶을때...

1. class에 static으로 thread 함수 정의 (ex. ThreadProc)
2. _beginthredex시 현재 자신의 instance(this)를 인자로 넘겨줌.
3. 넘겨 받은 this를 통해 실제 작업할 함수 호출 (ex. RealThreadProc)

#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <process.h>

#pragma comment(lib, "Kernel32.lib")

using namespace std;
class CTest
{
public:
CTest(void);
~CTest(void);
void beginThread(void);

HANDLE hThread;
int mParam;

protected:
//0. thread 함수 정의
static UINT WINAPI ThreadProc(LPVOID lpParam);
UINT WINAPI RealThreadProc(void);
};

CTest::CTest(void)  {}
CTest::~CTest(void) {}

void CTest::beginThread(void)  
{
//1. thread 실행
hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, (LPVOID)this, 0, NULL);
}

UINT WINAPI CTest::ThreadProc(LPVOID lpParam)
{
//2. 해당 인스턴스의 실제 thread 용 함수 호출
CTest* Test = (CTest*)lpParam;
return Test->RealThreadProc();
}

UINT CTest::RealThreadProc(void)
{
//3. 실제 thread 작업 수행
while (TRUE)
{
_tprintf(_T("param : %d\n"), mParam);
Sleep(1000);
}
}

int main()
{
int n=2;
HANDLE* hThread = new HANDLE[n];
CTest* test = new CTest[n];

for (int i=0; i<n; i++)
{
test[i].mParam = i;
test[i].beginThread();
hThread[i] = test[i].hThread;
}

//thread 종료 대기
WaitForMultipleObjects(2, hThread, TRUE, INFINITE);

for (int i=0; i<n; i++)
{
CloseHandle(hThread[i]);
delete test;
}
return 0;
}