#define AFXBEGINTHREAD(pfnThreadProc,pParam) AfxBeginThread(pfnThreadProc, pParam, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL)
CWinThread* g_pThread = NULL;
volatile bool g_bExitThread = true; // 이벤트를 써도 됨
CCriticalSection g_csExitThread;
// Create....
g_csExitThread.Lock();
g_bExitThread = false;
g_csExitThread.Unlock();
g_pThread = AFXBEGINTHREAD(ThreadFunc,아무값);
if(NULL == g_pThread)
{
// error...
}
g_pThread->m_bAutoDelete = FALSE;
g_pThread->ResumeThread();
// Destroy...
g_csExitThread.Lock();
g_bExitThread = true;
g_csExitThread.Unlock();
if(NULL != g_pThread)
{
::WaitForSingleObject(g_pThread->m_hThread,INFINITE);
delete g_pThread;
g_pThread = NULL;
}
// Thread Function...
UINT ThreadFunc(LPVOID lpParam)
{
while(true)
{
Sleep(1);
g_csExitThread.Lock();
if(true == g_bExitThread)
{
g_csExitThread.Unlock();
break;
}
else
{
g_csExitThread.Unlock();
// 작업...
}
}
return 0L;
}
댓글