C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件<thread>、<mutex>、<atomic>、<future>等用于支持多线程。

使用C++11开启一个线程是比较简单的,下面来看一个简单的例子:

#include <thread>

#include <iostream>

void hello()

{

std::cout << "Hello from thread " << std::endl;

}

int main()

{

std::thread t1(hello);

t1.join();

std::cout<<"Main Thread"<<std::endl;

return 0;

}

运行结果:

说明,通过thread 类直接申明一个线程t1,参数是这个线程执行的回调函数的地址,通过jion()方法阻塞主线程,直到t1线程执行结束为止。

C++11支持Lambda表达式,因此一个新线程的回调函数也可以是有一个Lambda表达式的形式,但是注意如果使用Lambda表达式最好不要使用引用的方式,应该使用值传递的方式来访问数据,在多线程中使用引用容易造成混乱。下面这个例子稍微复杂,创建了多个子线程,并使用了get_id()方法来获取当前线程的id。

#include <thread>

#include <iostream>

#include <vector>

int main()

{

std::vector<std::thread> threads;

for(int i = 0; i < 5; ++i){

threads.push_back(std::thread([](){

std::cout << "Hello from lamda thread " << std::this_thread::get_id() << std::endl;

}));

}

for(auto& thread : threads){

thread.join();

}

std::cout<<"Main Thread"<<"\t"<<std::this_thread::get_id()<<std::endl;

return 0;

}

运行结果:

上述代码中,使用vector来存放每个线程,线程的回调函数通过Lambda表达式产生,注意后面join的使用方式。

可以通过sleep_for来使线程睡眠一定的时间:

#include <thread>

#include <iostream>

#include <mutex>

using namespace std;

int main()

{

std::mutex m;

thread t1([&m]()

{

std::this_thread::sleep_for (chrono::seconds(10));

for(int i=0;i<10;i++)

{

m.lock();

cout <<  "In t1 ThreadID : " << std::this_thread::get_id() << ":" << i << endl;

m.unlock ();

}

} );

thread t2([&m]()

{

std::this_thread::sleep_for (chrono::seconds(1));

for(int i=0;i<10;i++)

{

m.lock ();

cout <<  "In t2 ThreadID : " << std::this_thread::get_id() << ":" << i << endl;

m.unlock();

}

} );

t1.join();

t2.join();

cout<<"Main Thread"<<endl;

return 0;

}

运行结果:

可以看出,由于线程t1睡眠的时间较长,t2先执行了。

延时有这几种类型:nanoseconds、microseconds、milliseconds、seconds、minutes、hours。

在使用多线程的程序中操作共享数据的时候一定要小心,由于线程的乱序执行,可能会得到意想不到的结果。通过下面的程序来看:

#include <thread>

#include <iostream>

#include <vector>

#include <mutex>

struct Counter {

std::mutex mutex;

int value;

Counter() : value(0) {}

void increment(){

// mutex.lock();                【1】表示没有使用锁

++value;

// mutex.unlock();              【1】

}

void decrement(){

mutex.lock();

--value;

mutex.unlock();

}

};

int main(){

Counter counter;

std::vector<std::thread> threads;

for(int i = 0; i < 5; ++i){

threads.push_back(std::thread([&](){

for(int i = 0; i < 10000; ++i){

counter.increment();

}

}));

}

for(auto& thread : threads){

thread.join();

}

std::cout << counter.value << std::endl;

return 0;

}

运行结果:

【1】

运行结果:(使用了锁)

说明:由于创建线程是使用lambda表达式,并使用引用的方式访问counter这个变量,当没有使用lock来保护的时候(情况【1】),执行的结果可能不像预期的5000(程序的意思是每个线程使counter中的value自加1000次,5个线程运行结束的时候应该是5000),当没有使用锁的时候自加的操作可能被其他线程打断,因此结果可能会小于5000。

C++11 多线程 教学(2)的更多相关文章

  1. C++11多线程教学(二)

    C++11多线程教学II 从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程 ...

  2. C++11多线程教学(一)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

  3. C++11多线程教学II

    从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程序,但是我们回避了线程同步 ...

  4. c++ 11 多线程教学(1)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

  5. C++11多线程教学

    转自:http://www.cnblogs.com/lidabo/p/3908705.html 本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads ...

  6. 【转】C++ 11 并发指南一(C++ 11 多线程初探)

    引言 C++ 11自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些C++ 11的新特性,算是记录一下自己学到的东西吧,和大家共勉. 相信Linux程序员都用过Pthrea ...

  7. C++11 并发指南一(C++11 多线程初探)

    引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...

  8. C++11 多线程编程 使用lambda创建std::thread (生产/消费者模式)

    要写个tcp server / client的博客,想着先写个c++11多线程程序.方便后面写博客使用. 目前c++11中写多线程已经很方便了,不用再像之前的pthread_create,c++11中 ...

  9. C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)

    前面八章介绍了 C++11 并发编程的基础(抱歉哈,第五章-第八章还在草稿中),本文将综合运用 C++11 中的新的基础设施(主要是多线程.锁.条件变量)来阐述一个经典问题——生产者消费者模型,并给出 ...

随机推荐

  1. i春秋30强挑战赛pwn解题过程

    80pts: 栈溢出,gdb调试发现发送29控制eip,nx:disabled,所以布置好shellcode后getshell from pwn import * #p=process('./tc1' ...

  2. JCo 指南

    http://blog.csdn.net/asdfak/article/details/5834731 JAVA 调用SAP端接口 Java Connector and BAPI 前些日子想去深入的研 ...

  3. C#类、静态类

    C#是面向对象(object-oriented)类型的计算机语言,使用类(class)来体现面向对象的概念. 分类(classification) 我们在现实世界中,会遇到很多被分类的事物.例如,动物 ...

  4. map的使用方法

    package cn.stat.p8.map.demo; import java.util.Collection; import java.util.HashMap; import java.util ...

  5. HTML常见标签学习与笔记总结

    HTML其实就是把页面的数据封装并加上标签 表头<head> <title> 浏览器标题栏显示的内容 <base> 有href和target属性,href指定网页中 ...

  6. Rufus-Create bootable USB drives the easy way

    Rufus Create bootable USB drives the easy way Rufus is a utility that helps format and create bootab ...

  7. 免小号QQ空间说说刷赞器

    小伙伴们赶紧用等待免小号QQ空间说说刷赞器,几分钟就可以刷好几百赞了噢, 给大家一个下载地址:http://www.dedewl.com/TA/gotoB.php?id=770319205B

  8. cf Magic Numbers

    http://codeforces.com/contest/320/problem/A #include <cstdio> #include <cstring> using n ...

  9. PHP安装OPENSSL扩展模块

    新项目上线时,PHP开发同事反映邮件功能不能正常使用. 原来是用465的SMTP加密端口,不是25端口.那要为当前的PHP安装OPENSSL扩展啦. 还好,网上有很多,弄一个过来就搞定. http:/ ...

  10. Leetcode_ Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...