多线程之线程状态

任务

  判断线程是否结束运行

代码

#include "windows.h"
#include "iostream"
#include "atlstr.h"
using namespace std;

DWORD WINAPI thread(LPVOID);

int main(int argc, char **argv) {
	HANDLE thread1, thread2;
	DWORD exit_code1 = 0, exit_code2 = 0;
	DWORD thread_id1, thread_id2;
	thread1 = CreateThread(NULL, 0, thread, (LPVOID)1, 0, &thread_id1);
	if (thread1)cout << "Thread 1 launched\n";
	thread2 = CreateThread(NULL, 0, thread, (LPVOID)1, 0, &thread_id2);
	if (thread2) {
		ResumeThread(thread2);        //使线程进入运行状态
		cout << "Thread 2 launched\n";
	}
	for (;;) {
		GetExitCodeThread(thread1, &exit_code1);
		GetExitCodeThread(thread2, &exit_code2);
		if (exit_code1 == STILL_ACTIVE)
			cout << "Thread1 is running" << endl;
		else 
			cout << "线程1退出码:" << exit_code1 << endl;
		if (exit_code2 == STILL_ACTIVE)
			cout << "Thread2 is runnging" << endl;
		else
			cout << "线程2退出码:" << exit_code2 << endl;
		if (exit_code1 != STILL_ACTIVE && exit_code2 != STILL_ACTIVE) {
			break;
		}
	}
	return 0;
}

DWORD WINAPI thread(LPVOID n) {
	int m = (DWORD)n;
	Sleep(10 * (5 - m));
	return m * 10;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值