c++ 11 thread使用
2015-09-15 14:34
441 查看
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> std::mutex mtx; void foo() { int i=0; while(i++<100) { mtx.lock(); sleep(3); printf("foo %d\n",i); mtx.unlock(); sleep(1); } } void bar(int x) { int i=100; while(i++<200) { mtx.lock(); printf("bar %d\n",i); mtx.unlock(); sleep(1); } } int main() { std::thread first (foo); // spawn new thread that calls foo() std::thread second (bar,0); // spawn new thread that calls bar(0) std::cout << "main, foo and bar now execute concurrently...\n"; first.join(); // pauses until first finishes second.join(); // pauses until second finishes std::cout << "foo and bar completed.\n"; return 0; }
相关文章推荐
- C++中的struct专题研究
- C++回顾之static与单例模式
- C++学习之分解质因数
- C语言学习手记-一元二次函数解法
- C++ Primer Plus(第六版)--学习杂记(第三章)
- C++实现《面向对象之设计模式——单身模式》
- 程序调试VC++ 6.0
- loadrunner icrosoft Visual c++2005 sp1运行时组件,就会提示命令行选项语法错误,键入“命令/?”
- 为什么要用C++编写服务端程序?
- c/c++ 对于浮点型的判断
- 用C++品尝Vista美味:界面的毛玻璃效果——涨姿势
- C++基础知识总结-类的基本知识
- C语言link过程详解(多文件编译过程)
- C++程序设计实践题1
- c++builder6.0 mdi窗体+自定义子窗体
- C++如何返回函数指针
- qt4编译c++11文件
- C++中虚函数的实现原理
- C++程序中尽可能使用const
- 【C/C++学院】0804-C语言和设计模式(继承、封装、多态)