您的位置:首页 > 编程语言 > C语言/C++

C++多线程例子

2013-11-25 14:35 162 查看
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
int i=0;
while(i<20)
{
//cout<<"I am from a thread ,count="<<i++<<endl;
printf("I am from a thread ,count=%d\n",i++);
Sleep(500);
}
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThread;
DWORD dwThreadId;
//创建一个线程
hThread=::CreateThread(
NULL,//默认安全属性
NULL,//默认堆栈大小
ThreadProc,//线程入口地址
NULL,//传给函数的参数
0,//指定线程立即运行
&dwThreadId);//返回线程的ID号
//cout<<"Now another thread has been create.ID="<<dwThreadId<<endl;
printf("Now another thread has been create.ID=%d\n",dwThreadId);
printf("线程已开始......\n");
::WaitForSingleObject(hThread,INFINITE);
::CloseHandle(hThread);
printf("线程已结束......");
return 0;

}


有兴趣的话对比一下cout和printf的区别,本人无意中发现的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: