C++多线程

#include <windows.h>

#include <stdio.h>

#include <iostream>

#include <conio.h>




using namespace std;




DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data

DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data




int time1 = 200;

int time2 = 500;




void main()

{

HANDLE hThread1;

HANDLE hThread2;

//创建线程




hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);

hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);




while (1)

{ 

Sleep(1000);

cout << endl;

}




CloseHandle(hThread1);

CloseHandle(hThread2);










Sleep(40000);

}




//线程1的入口函数

DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data

{

while (true)

{

Sleep(time1);

cout << 1;

}




return 0;

}




//线程2的入口函数

DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data

{

while (true)

{

Sleep(time2);

cout << 2;

}




return 0;

}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注