// 2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
#include "stdlib.h"
DWORD WINAPI thr1(LPVOID lp)//参数可以给线程传递一些数据,作为内部使用,很重要
{
while(true)
{
cout<<"Hello,thread!____1___"<<endl;
Sleep(1000);
}
return 0;
}
DWORD WINAPI thr2(LPVOID lp)
{
while(true)
{
cout<<"Hello,thread!____2___"<<endl;
Sleep(1000);
}
return 0;
}
VOID threa()
{
DWORD nid1=0,nid2=0;
HANDLE hd1=CreateThread(NULL,0,thr1,NULL,0,&nid1);
WaitForSingleObject(hd1,2000);
HANDLE hd2=CreateThread(NULL,0,thr2,NULL,0,&nid2);
CloseHandle(hd1);//关闭句柄,释放资源,并不是结束线程
CloseHandle(hd2);
}
int main(int argc, char* argv[])
{
threa();
getchar();
return 0;
}
******************************************************************
#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
DWORD WINAPI pro(LPVOID lp)
{
cout<<"Hello,thread!"<<endl;
return 0;
}
int main(int argc, char* argv[])
{
DWORD nid=0;
HANDLE hd;
hd=CreateThread(NULL,0,pro,NULL,0,&nid);
WaitForSingleObject(hd,INFINITE);//如果没有这个函数的话,会出现的情况就是自己创建的线程无法执行
CloseHandle(hd);
return 0;
}
//每个进程中都有一个看不见的主线程,在考虑的时候不能遗漏,否则会出现线程调度的问题