C++多线程入门

C++多线程入门

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>


DWORD WINAPI run(LPVOID p)
{
printf("three%d run../n",p);
Sleep((DWORD)p*3000);
return (DWORD)p;
}
int main()
{
HANDLE three1;    //线程
HANDLE three2;
DWORD uExitCode1;   // 返回值
DWORD uExitCode2;
DWORD dwThId,dwThId1;    // 线程id号

three1=CreateThread(NULL,0,run,(LPVOID)1,0,&dwThId);
three2=CreateThread(NULL,0,run,(LPVOID)2,0,&dwThId1);
int c=0;
for (;;)
{
   puts("press any key to check thread ...");
   _getch();
   //'GetExitCodeThread'函数来判断一个线程是否还在执行 说明 获取一个已      中止线程的退出代码 返回值 bool,非零表示成功,零表示失败。...
   GetExitCodeThread(three2,&uExitCode2);
   if (!c){
  
   GetExitCodeThread(three1,&uExitCode1);
  

   if(uExitCode1==STILL_ACTIVE)
    puts("Thread1 still active");
   else
   {
    printf("Thread1 over with: %d/n",dwThId);
    CloseHandle(three1);
    c=1;
    printf("Thread1 over with: %d/n",dwThId);
   }
   }
   if(uExitCode2==STILL_ACTIVE)
    puts("Thread2 still active");
   else
    printf("Thread2 over with: %d/n",dwThId1);
   if(uExitCode1!=STILL_ACTIVE && uExitCode2!=STILL_ACTIVE)
    break;
}
// CloseHandle(three1); // 关闭线程句柄并不影响线程的执行
CloseHandle(three2);
return EXIT_SUCCESS;
}

http://www.brsbox.com/filebox/down/fc/d79033016d6b89837f0ed22833f3b7ee


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值