ExitThread

本文详细介绍了如何使用 C++ 中的 ExitThread 和 GetExitCodeThread 函数来安全地检查线程状态并退出线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <windows.h>
DWORD WINAPI ThreadProc(  LPVOID lpParameter);
void Test(void);
//ExitThread函数和GetExitCodeThread配合使用,安全的确定线程是否执行完毕
int main (void)
{

	DWORD IDThread=0;
	DWORD ExitCode1=0;
	HANDLE hThread1=CreateThread(NULL,NULL,ThreadProc,(LPVOID)1,NULL,&IDThread);
	while(true)
	{
		BOOL rc=false;
		rc=GetExitCodeThread(hThread1,&ExitCode1);
		if (rc && ExitCode1!=STILL_ACTIVE)
		{
			break;
		}

	}
	CloseHandle(hThread1);
	printf("thread1 %d\n",ExitCode1);




	getchar();

	return 0;
}
DWORD WINAPI  ThreadProc(  LPVOID lpParameter)
{
	printf("ThreadProc Ruing!\n"); 
	Test();
	return true;
}

void Test(void)
{
	printf("About to exit thread\n");
	ExitThread(4);
	printf("this will never print\n");
	return;
}
/*
2015年5月5日 21:32:22
程序执行结果如下:
ThreadProc Ruing!
About to exit thread
thread1 4
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值