WaitForMultipleObjects、WaitForSingleObject、GetExitCodeThread
WaitForMultipleObjects、WaitForSingleObject、GetExitCodeThread
// Thread.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
HANDLE hth[2];
DWORD WINAPI func1(LPVOID p1)
{
printf("fun1 end\n");
DWORD exitC;
GetExitCodeThread(hth[1],&exitC);
if (exitC==STILL_ACTIVE)//还在运行中
{
printf("func2 STILL_ACTIVE\n");
}
return 4;
}
DWORD WINAPI func2(LPVOID p2)
{
Sleep(1000);
DWORD exitC;
GetExitCodeThread(hth[0],&exitC);
printf("func1 exit code= %d\nfun2 end\n",exitC);
return 0;
}
int main(int argc, char* argv[])
{
DWORD thID1,thID2;
printf("Hello World!\n");
hth[0]= CreateThread(NULL,0,func1,0,0,&thID1);
hth[1]= CreateThread(NULL,0,func2,0,0,&thID2);
WaitForMultipleObjects(2,hth,true,INFINITE); //true等待所有线程结束 false 只要有一条线程结束就退出 INFINITE一直等待
//WaitForMultipleObjects(2,hth,false,10000);//INFINITE
//WaitForSingleObject(hth[1],INFINITE);
return 0;
}
//
#include "stdafx.h"
#include <windows.h>
HANDLE hth[2];
DWORD WINAPI func1(LPVOID p1)
{
printf("fun1 end\n");
DWORD exitC;
GetExitCodeThread(hth[1],&exitC);
if (exitC==STILL_ACTIVE)//还在运行中
{
printf("func2 STILL_ACTIVE\n");
}
return 4;
}
DWORD WINAPI func2(LPVOID p2)
{
Sleep(1000);
DWORD exitC;
GetExitCodeThread(hth[0],&exitC);
printf("func1 exit code= %d\nfun2 end\n",exitC);
return 0;
}
int main(int argc, char* argv[])
{
DWORD thID1,thID2;
printf("Hello World!\n");
hth[0]= CreateThread(NULL,0,func1,0,0,&thID1);
hth[1]= CreateThread(NULL,0,func2,0,0,&thID2);
WaitForMultipleObjects(2,hth,true,INFINITE); //true等待所有线程结束 false 只要有一条线程结束就退出 INFINITE一直等待
//WaitForMultipleObjects(2,hth,false,10000);//INFINITE
//WaitForSingleObject(hth[1],INFINITE);
return 0;
}