TerminateThread The TerminateThread function terminates a thread. BOOL TerminateThread( HANDLE hThread, // handle to the thread DWORD dwExitCode // exit code for the thread );
HANDLE handle; DWORD dwExitCode; int nRet; //======== //CreateThread的部分,省略 //======== if (handle != NULL) { GetExitCodeThread(handle, &dwExitCode); nRet=CloseHandle(handle); }
TerminateThread
The TerminateThread function terminates a thread. BOOL TerminateThread(
HANDLE hThread, // handle to the thread
DWORD dwExitCode // exit code for the thread
);
DWORD dwExitCode;
int nRet;
//========
//CreateThread的部分,省略
//========
if (handle != NULL) {
GetExitCodeThread(handle, &dwExitCode);
nRet=CloseHandle(handle);
}
在线程外面如果想退出了,就bThread=true;
线程里面有个判断
if(bThread)
{
return 0 ;
}
HANDLE hThread, // handle to the thread
DWORD dwExitCode // exit code for the thread
);
robin_yao(认真)和SoLike(思危)的方法好
设全局变量使用Event和WaitForSingleObject
HANDLE hEventNext = CreateEvent(NULL, FALSE, FALSE, NULL);DWORD WINAPI ThreadFunc(PVOID pvParam)
{
enum{EVENT_EXIT, EVENT_NEXT, MAX_EVENT};
HANDLE hEvents[MAX_EVENT] = {hEventExit, hEventNext}; while (1)
{
DWORD dwRet = WaitForMultipleObjects(MAX_EVENT, hEvents, FALSE, INFINITE);
switch(dwRet - WAIT_OBJECT_0)
{
case EVENT_EXIT:
std::cout<< "退出线程" << std::endl;
return TRUE;
case EVENT_NEXT:
Sleep(1000);
std::cout<< "调用主线程函数" << std::endl;
break;
default:
break;
}
} return TRUE;
}void main()
{
HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, 0, 0, NULL); for(int i=0; i<2; i++)
{
SetEvent(hEventNext); // 通知辅助线程干活
Sleep(5000); // 干别的事
} std::cout<< "准备退出线程" << std::endl; SetEvent(hEventExit); // 通知辅助线程退出 DWORD dwRet = WaitForSingleObject(hThread, 15000); // 等待退出
if(dwRet == WAIT_TIMEOUT)
{
std::cout << "工作线程无法退出,强行杀死" << std::endl;
TerminateThread(hThread, 1);
}
CloseHandle(hThread);
CloseHandle(hEventNext);
CloseHandle(hEventExit);
}
SetEvent(hEventExit); // 通知辅助线程退出DWORD dwRet = WaitForSingleObject(hThread, 15000); // 等待退出
if(dwRet == WAIT_TIMEOUT)
{
std::cout << "工作线程无法退出,强行杀死" << std::endl;
TerminateThread(hThread, 1);
}
CloseHandle(hThread);
是采用阻塞的方式判断线程退出。
HANDLE g_hEvent;int WINAPI WinMain(...)
{
//Create the manual-reset, nonsignaled event.
g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //Spawn 3 new threads.
HANDLE hThread[3];
DWORD dwThreadID;
hThread[0] = _beginthreadex(NULL, 0, WordCount, NULL, 0, &dwThreadID);
hThread[1] = _beginthreadex(NULL, 0, SpellCheck, NULL, 0, &dwThreadID);
hThread[2] = _beginthreadex(NULL, 0, GrammarCheck, NULL, 0, &dwThreadID); OpenFileAndReadContentsIntoMemory(...); //Allow all 3 threads to access the memory.
SetEvent(g_hEvent);
...
}
DWORD WINAPI WordCount(PVOID pvParam)
{
//Wait until the file's data is in memory.
WaitForSingleObject(g_hEvent, INFINITE); //Access the memory block.
...
SetEvent(g_hEvent);
return(0);
}DWORD WINAPI SpellCheck(PVOID pvParam)
{
//Wait until the file's data is in memory.
WaitForSingleObject(g_hEvent, INFINITE); //Access the memory block.
...
SetEvent(g_hEvent);
return(0);
}DWORD WINAPI GrammarCheck(PVOID pvParam)
{
//Wait until the file's data is in memory.
WaitForSingleObject(g_hEvent, INFINITE); //Access the memory block.
...
SetEvent(g_hEvent);
return(0);
}