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的区别,本人无意中发现的。
相关文章推荐
- c++编写的多线程售票的例子
- C++多线程 例子
- 通过例子学设计模式之--单例模式以及多线程下说明(C++实现)
- 很好很有爱的C++多线程例子
- c++实现多线程简单例子
- C++进阶—>互斥量:Event控制:多线程实现生产者-消费者例子
- C++多线程的经典例子
- C++使用临界区(互斥体)来控制多线程访问同一个变量(经典例子)
- mfc c++ 多线程AfxBeginThread 例子( 一)
- C++多线程 例子
- C++多线程编程例子 Demo 关键代码段 临界区 实现线程同步
- 一个C++调用C的例子
- c++中的new操作符定位用法(placement new) 的一个例子
- C++练笔例子1
- python多线程 简单例子
- JAVA多线程编程详解-详细操作例子
- Ajax - JavaScript之实现私有属性、像C++和Java一样支持基于类的继承方法之例子
- 【转】c++ 多线程
- 浅析C++多线程内存模型
- c# winform多线程的小例子