由本身线程退出
VOID ExitProcess(UNIT fuExitCode),不被推荐使用
系统将结束主线程,不管进程是否还有其它线程执行,而直接结束进程。
ExitThread与之不同的是只结束主线程,如果还有其它线程,则进程继续执行。
由其他进程结束,异步运行函数
BOOL TerminateProcess(HANDLE hProcess, UNIT fuExitCode)
多数的进程结束方法应是调用CloseHandle。
/*
* ExitThrd.c
*
* Sample code for "Multithreading Applications in Win32"
* This is from Chapter 2, Listing 2-3
*
* Demonstrate ExitThread
*/
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD WINAPI ThreadFunc(LPVOID);
void AnotherFunc(void);
int main()
{
HANDLE hThrd;
DWORD exitCode = 0;
DWORD threadId;
hThrd = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)1,
0,
&threadId );
if (hThrd)
printf("Thread launched\n");
printf("About to exit Main thread,return 1\n");
//exit(1);
ExitThread(1);//exit Main thread,return 1
for(;;)
{
BOOL rc;
rc = GetExitCodeThread(hThrd, &exitCode);
if (rc && exitCode != STILL_ACTIVE )
break;
}
CloseHandle(hThrd);
printf("Thread returned %d\n", exitCode);
return EXIT_SUCCESS;
}
/*
* Call a function to do something that terminates
* the thread with ExitThread instead of returning.
*/
DWORD WINAPI ThreadFunc(LPVOID n)
{
printf("Thread running\n");
printf("About to exit thread,return 5\n");
ExitThread(5);
printf("Under this will never execute!\n");
AnotherFunc();
return 0;
}
void AnotherFunc()
{
printf("About to exit thread\n");
ExitThread(4);
// It is impossible to get here, this line
// will never be printed
printf("This will never print\n");
}