直接上代码,功能留在以后有时间再写
// CThreadTestDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <process.h>
#include <Windows.h>
void ThreadProc(void *param);
int _tmain(int argc, _TCHAR* argv[])
{
int i1=1;
int i2=2;
int i3=3;
int i4=4;
int i5=5;
HANDLE hanle;
HANDLE hanle0;
HANDLE hanle1;
HANDLE hanle2;
HANDLE hanle3;
printf("\t Thread Demo \n");
//线程被创建的时候几乎是同一个时间点
hanle=(HANDLE)_beginthread(ThreadProc,0,&i1);
hanle0=(HANDLE)_beginthread(ThreadProc,0,&i2);
hanle1=(HANDLE)_beginthread(ThreadProc,0,&i3);
hanle2=(HANDLE)_beginthread(ThreadProc,0,&i4);
hanle3=(HANDLE)_beginthread(ThreadProc,0,&i5);
WaitForSingleObject(hanle,INFINITE);
return 0;
}
void ThreadProc(void *param)
{
int h=*((int*)param);
printf("The %d Thread is Running\n",h);
if (h==5)
{
Sleep(3000);
}
if (h==2)
{
Sleep(4000);
}
if (h==3)
{
Sleep(5000);
}
if (h==4)
{
Sleep(6000);
}
if (h==1)
{
Sleep(7000);
}
printf("The %d Thread is Close\n",h);//线程结束是按照各自的线程中的程序运行情况而定
_endthread();
}