结束一个线程 GetExitCodeThread() 是判断线程函数的结束而结束线程。有时候可能需要更强制性的手法结束一个线程 可以使用 ExitThread().
VOID ExitThread(
DWORD dwExitCode
);
参数说明:
dwExitCode 指定此线程之结束代码。
返回值:没有 此函数从不返回。
程序示例:
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD WINAPI ThreadFunc(LPVOID);
void AnotherFunc(void);
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThrd;
DWORD exitCode = 0;
DWORD threadId;
hThrd = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)1,
0,
&threadId );
if (hThrd)
printf("Thread launched/n");
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");
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");
}
CloseHandle 的重要性
BOOL CloseHandle(
HANDLE hObject
);
参数说明:
hObject 代表一个以打开之对象 handle
返回值:
如果成功 CloseHandle() 返回TRUE ,如果失败则返回FALSE,此时可以调用GetLastError()获知失败原因。
等待一个线程的结束:
DWORD WaitForSingleObject(
HANDLE hHandle,
SWORD dwMilliseconds
);
参数说明:
hHandle:等待对象的 handle (代表一个核心对象)。在本例中此为线程 handle
dwMilliseconds: 等待的最长时间。时间终了即使 handle 尚未成为激发状态,此函数还是要返回,此值可以是0(代表立刻返回) 也可以是INFINITE 代表无穷等待。
返回值:
如果函数失败,则传回WAIT_FAILED, 这时候你可以调用 GetLastError()取得更多信息。
1、等待的目标(核心对象)变成激发状态,这种情况下返回值将为 WAIT_OBJECT_0。
2、核心对象变为激发状态之前,等待时间终了,这种情况下返回值将为 WAIT_TIMEOUT。
3、如果一个拥有 mutex(互斥器)的线程结束前没有释放 mutex,则传回WAIT_ABANDONED。
获得一个线程对象的 handle 之后,WaitForSingleObject()要求操作系统让线程1睡觉,直到以下任何一种情况发生:
线程2结束、dwMilliseconds 时间终了,该值系从函数调用后开始计算。
for(;;)
{
BOOL rc;
rc = GetExitCodeThread(hThrd, &exitCode);
if (rc && exitCode != STILL_ACTIVE )
break;
}等同于 WaitForSingleObject(
hThrd,
INFINITE
);
VOID ExitThread(
DWORD dwExitCode
);
参数说明:
dwExitCode 指定此线程之结束代码。
返回值:没有 此函数从不返回。
程序示例:
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD WINAPI ThreadFunc(LPVOID);
void AnotherFunc(void);
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThrd;
DWORD exitCode = 0;
DWORD threadId;
hThrd = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)1,
0,
&threadId );
if (hThrd)
printf("Thread launched/n");
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");
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");
}
CloseHandle 的重要性
BOOL CloseHandle(
HANDLE hObject
);
参数说明:
hObject 代表一个以打开之对象 handle
返回值:
如果成功 CloseHandle() 返回TRUE ,如果失败则返回FALSE,此时可以调用GetLastError()获知失败原因。
等待一个线程的结束:
DWORD WaitForSingleObject(
HANDLE hHandle,
SWORD dwMilliseconds
);
参数说明:
hHandle:等待对象的 handle (代表一个核心对象)。在本例中此为线程 handle
dwMilliseconds: 等待的最长时间。时间终了即使 handle 尚未成为激发状态,此函数还是要返回,此值可以是0(代表立刻返回) 也可以是INFINITE 代表无穷等待。
返回值:
如果函数失败,则传回WAIT_FAILED, 这时候你可以调用 GetLastError()取得更多信息。
1、等待的目标(核心对象)变成激发状态,这种情况下返回值将为 WAIT_OBJECT_0。
2、核心对象变为激发状态之前,等待时间终了,这种情况下返回值将为 WAIT_TIMEOUT。
3、如果一个拥有 mutex(互斥器)的线程结束前没有释放 mutex,则传回WAIT_ABANDONED。
获得一个线程对象的 handle 之后,WaitForSingleObject()要求操作系统让线程1睡觉,直到以下任何一种情况发生:
线程2结束、dwMilliseconds 时间终了,该值系从函数调用后开始计算。
for(;;)
{
BOOL rc;
rc = GetExitCodeThread(hThrd, &exitCode);
if (rc && exitCode != STILL_ACTIVE )
break;
}等同于 WaitForSingleObject(
hThrd,
INFINITE
);