转 C++11 并发指南系列

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               



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


引言

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

相信 Linux 程序员都用过 Pthread, 但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++11 的多线程编程方式还是很有益处的。

如果你对 C++11 不太熟悉,建议先看看维基百科上关于 C++11 新特性的介绍,中文C++11介绍英文C++11介绍 ,另外C++之父 Bjarne Stroustrup 的关于 C++11 的 FAQ 也是必看的,我也收集了一些关于C++11的资料,供大家查阅:

资料汇

http://www.open-std.org/jtc1/sc22/wg21/

C++0x/C++11 Support in GCC:http://gcc.gnu.org/projects/cxx0x.html

What is C++0x:https://www2.research.att.com/~bs/what-is-2009.pdf

Overview of the New C++http://www.artima.com/shop/overview_of_the_new_cpp

Overview of the New C++ (C++0x).pdf:http://ishare.iask.sina.com.cn/f/20120005.html?from=like

A Brief Look at C++0xhttp://www.artima.com/cppsource/cpp0x.html

Summary of C++11 Feature Availability in gcc and MSVC:http://www.aristeia.com/C++11/C++11FeatureAvailability.htm

C++ 11: Come Closer:http://www.codeproject.com/Articles/344282/Cplusplus-11-Come-Closer

C++11 threads, locks and condition variables: http://www.codeproject.com/Articles/598695/Cplusplus11-threads-locks-and-condition-variables

Move Semantics and Perfect Forwarding in C++11:http://www.codeproject.com/Articles/397492/Move-Semantics-and-Perfect-Forwarding-in-Cplusplus

http://solarianprogrammer.com/categories/C++11/

C++11 Concurrency:http://www.baptiste-wicht.com/2012/03/cpp11-concurrency-part1-start-threads/

http://www.hpl.hp.com/personal/Hans_Boehm/misc_slides/sfacm-cleaned.pdf

http://en.cppreference.com/w/cpp/thread

http://isocpp.org/blog/2012/12/c11-a-cheat-sheet-alex-sinyakov

The Biggest Changes in C++11:http://blog.smartbear.com/c-plus-plus/the-biggest-changes-in-c11-and-why-you-should-care/

Ten C++11 Features Every C++ Developer Should Use:http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer

 C++11 – A Glance [part 1 of n]:http://www.codeproject.com/Articles/312029/Cplusplus11-A-Glance-part-1-of-n

 C++11 – A Glance [part 2 of n]:http://www.codeproject.com/Articles/314415/Cplusplus11-A-Glance-part-2-of-n

C++11(及现代C++风格)和快速迭代式开发:http://mindhacks.cn/2012/08/27/modern-cpp-practices/

Lambda Functions in C++11 - the Definitive Guide:http://www.cprogramming.com/c++11/c++11-lambda-closures.html

Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint:http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

Rvalue-references-and-move-semantics-in-c++11:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html

http://www.gotw.ca/publications/index.htm

http://www.devx.com/SpecialReports/Door/38865

Multi-threading in C++0x:http://accu.org/index.php/journals/1584

C++ 0X feature summary cheat sheat:http://www.iesensor.com/blog/2011/05/31/c-0x-feature-summary-cheat-sheat/

Multithreading in C++0x part 1: Starting Threads:http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-1-starting-threads.html

http://en.cppreference.com/w/cpp/thread

http://www.cplusplus.com/reference/multithreading/

好了,下面来说正题吧 ;-)

与 C++11 多线程相关的头文件

C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。

  • <atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
  • <thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
  • <mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。
  • <condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。
  • <future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。

std::thread "Hello world"

下面是一个最简单的使用 std::thread 类的例子:

复制代码
#include <stdio.h>#include <stdlib.h>#include <iostream> // std::cout#include <thread>   // std::threadvoid thread_task() {    std::cout << "hello thread" << std::endl;}/* * ===  FUNCTION  ========================================================= *         Name:  main *  Description:  program entry routine. * ======================================================================== */int main(int argc, const char *argv[]){    std::thread t(thread_task);    t.join();    return EXIT_SUCCESS;}  /* ----------  end of function main  ---------- */
复制代码

Makefile 如下:

复制代码
all:ThreadCC=g++CPPFLAGS=-Wall -std=c++11 -ggdbLDFLAGS=-pthreadThread:Thread.o    $(CC) $(LDFLAGS) -o $@ $^Thread.o:Thread.cc    $(CC) $(CPPFLAGS) -o $@ -c $^.PHONY:    cleanclean:    rm Thread.o Thread
复制代码

注意在 Linux GCC4.6 环境下,编译时需要加 -pthread,否则执行时会出现:

$ ./Threadterminate called after throwing an instance of 'std::system_error'  what():  Operation not permittedAborted (core dumped)

原因是 GCC 默认没有加载 pthread 库,据说在后续的版本中可以不用在编译时添加 -pthread 选项。

更多的有关 C++11 Concurrency 的介绍将在后续的一系列博客中写出,希望自己勤快一点吧 ;-)



C++11 并发指南二(std::thread 详解)



上一篇博客《C++11 并发指南一(C++11 多线程初探)》中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用法。

std::thread 在 <thread> 头文件中声明,因此使用 std::thread 时需要包含 <thread> 头文件。

std::thread 构造

default (1)
thread() noexcept;
initialization (2)
template <class Fn, class... Args>explicit thread (Fn&& fn, Args&&... args);
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;
  • (1). 默认构造函数,创建一个空的 thread 执行对象。
  • (2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
  • (3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
  • (4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。
  • 注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

std::thread 各种构造函数例子如下(参考):

复制代码
#include <iostream>#include <utility>#include <thread>#include <chrono>#include <functional>#include <atomic> void f1(int n){    for (int i = 0; i < 5; ++i) {        std::cout << "Thread " << n << " executing\n";        std::this_thread::sleep_for(std::chrono::milliseconds(10));    }} void f2(int& n){    for (int i = 0; i < 5; ++i) {        std::cout << "Thread 2 executing\n";        ++n;        std::this_thread::sleep_for(std::chrono::milliseconds(10));    }} int main(){    int n = 0;    std::thread t1; // t1 is not a thread    std::thread t2(f1, n + 1); // pass by value    std::thread t3(f2, std::ref(n)); // pass by reference    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread    t2.join();    t4.join();    std::cout << "Final value of n is " << n << '\n';}
复制代码

move 赋值操作

move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;
  • (1). move 赋值操作,如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则 terminate() 报错。
  • (2). 拷贝赋值操作被禁用,thread 对象不可被拷贝。

请看下面的例子:

复制代码
#include <stdio.h>#include <stdlib.h>#include <chrono>    // std::chrono::seconds#include <iostream>  // std::cout#include <thread>    // std::thread, std::this_thread::sleep_forvoid thread_task(int n) {    std::this_thread::sleep_for(std::chrono::seconds(n));    std::cout << "hello thread "        << std::this_thread::get_id()        << " paused " << n << " seconds" << std::endl;}/* * ===  FUNCTION  ========================================================= *         Name:  main *  Description:  program entry routine. * ======================================================================== */int main(int argc, const char *argv[]){    std::thread threads[5];    std::cout << "Spawning 5 threads...\n";    for (int i = 0; i < 5; i++) {        threads[i] = std::thread(thread_task, i + 1);    }    std::cout << "Done spawning threads! Now wait for them to join\n";    for (auto& t: threads) {        t.join();    }    std::cout << "All threads joined.\n";    return EXIT_SUCCESS;}  /* ----------  end of function main  ---------- */
复制代码

其他成员函数



C++11 并发指南三(std::mutex 详解)



上一篇《C++11 并发指南二(std::thread 详解)》中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法。

Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文件中,所以如果你需要使用 std::mutex,就必须包含 <mutex> 头文件。

<mutex> 头文件介绍

Mutex 系列类(四种)
  • std::mutex,最基本的 Mutex 类。
  • std::recursive_mutex,递归 Mutex 类。
  • std::time_mutex,定时 Mutex 类。
  • std::recursive_timed_mutex,定时递归 Mutex 类。
Lock 类(两种)
  • std::lock_guard,与 Mutex RAII 相关,方便线程对互斥量上锁。
  • std::unique_lock,与 Mutex RAII 相关,方便线程对互斥量上锁,但提供了更好的上锁和解锁控制。
其他类型
  • std::once_flag
  • std::adopt_lock_t
  • std::defer_lock_t
  • std::try_to_lock_t
函数
  • std::try_lock,尝试同时对多个互斥量上锁。
  • std::lock,可以同时对多个互斥量上锁。
  • std::call_once,如果多个线程需要同时调用某个函数,call_once 可以保证多个线程对该函数只调用一次。

std::mutex 介绍

下面以 std::mutex 为例介绍 C++11 中的互斥量用法。

std::mutex 是C++11 中最基本的互斥量,std::mutex 对象提供了独占所有权的特性——即不支持递归地对 std::mutex 对象上锁,而 std::recursive_lock 则可以递归地对互斥量对象上锁。

std::mutex 的成员函数
  • 构造函数,std::mutex不允许拷贝构造,也不允许 move 拷贝,最初产生的 mutex 对象是处于 unlocked 状态的。
  • lock(),调用线程将锁住该互斥量。线程调用该函数会发生下面 3 种情况:(1). 如果该互斥量当前没有被锁住,则调用线程将该互斥量锁住,直到调用 unlock之前,该线程一直拥有该锁。(2). 如果当前互斥量被其他线程锁住,则当前的调用线程被阻塞住。(3). 如果当前互斥量被当前调用线程锁住,则会产生死锁(deadlock)。
  • unlock(), 解锁,释放对互斥量的所有权。
  • try_lock(),尝试锁住互斥量,如果互斥量被其他线程占有,则当前线程也不会被阻塞。线程调用该函数也会出现下面 3 种情况,(1). 如果当前互斥量没有被其他线程占有,则该线程锁住互斥量,直到该线程调用 unlock 释放互斥量。(2). 如果当前互斥量被其他线程锁住,则当前调用线程返回 false,而并不会被阻塞掉。(3). 如果当前互斥量被当前调用线程锁住,则会产生死锁(deadlock)。

下面给出一个与 std::mutex 的小例子(参考

复制代码
#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutexvolatile int counter(0); // non-atomic counterstd::mutex mtx;           // locks access to countervoid attempt_10k_increases() {    for (int i=0; i<10000; ++i) {        if (mtx.try_lock()) {   // only increase if currently not locked:            ++counter;            mtx.unlock();        }    }}int main (int argc, const char* argv[]) {    std::thread threads[10];    for (int i=0; i<10; ++i)        threads[i] = std::thread(attempt_10k_increases);    for (auto& th : threads) th.join();    std::cout << counter << " successful increases of the counter.\n";    return 0;}
复制代码

std::recursive_mutex 介绍

std::recursive_mutex 与 std::mutex 一样,也是一种可以被上锁的对象,但是和 std::mutex 不同的是,std::recursive_mutex 允许同一个线程对互斥量多次上锁(即递归上锁),来获得对互斥量对象的多层所有权,std::recursive_mutex 释放互斥量时需要调用与该锁层次深度相同次数的 unlock(),可理解为 lock() 次数和 unlock() 次数相同,除此之外,std::recursive_mutex 的特性和 std::mutex 大致相同。

std::time_mutex 介绍

std::time_mutex 比 std::mutex 多了两个成员函数,try_lock_for(),try_lock_until()。

try_lock_for 函数接受一个时间范围,表示在这一段时间范围之内线程如果没有获得锁则被阻塞住(与 std::mutex 的 try_lock() 不同,try_lock 如果被调用时没有获得锁则直接返回 false),如果在此期间其他线程释放了锁,则该线程可以获得对互斥量的锁,如果超时(即在指定时间内还是没有获得锁),则返回 false。

try_lock_until 函数则接受一个时间点作为参数,在指定时间点未到来之前线程如果没有获得锁则被阻塞住,如果在此期间其他线程释放了锁,则该线程可以获得对互斥量的锁,如果超时(即在指定时间内还是没有获得锁),则返回 false。

下面的小例子说明了 std::time_mutex 的用法(参考)。

复制代码
#include <iostream>       // std::cout#include <chrono>         // std::chrono::milliseconds#include <thread>         // std::thread#include <mutex>          // std::timed_mutexstd::timed_mutex mtx;void fireworks() {  // waiting to get a lock: each thread prints "-" every 200ms:  while (!mtx.try_lock_for(std::chrono::milliseconds(200))) {    std::cout << "-";  }  // got a lock! - wait for 1s, then this thread prints "*"  std::this_thread::sleep_for(std::chrono::milliseconds(1000));  std::cout << "*\n";  mtx.unlock();}int main (){  std::thread threads[10];  // spawn 10 threads:  for (int i=0; i<10; ++i)    threads[i] = std::thread(fireworks);  for (auto& th : threads) th.join();  return 0;}
复制代码

std::recursive_timed_mutex 介绍

和 std:recursive_mutex 与 std::mutex 的关系一样,std::recursive_timed_mutex 的特性也可以从 std::timed_mutex 推导出来,感兴趣的同鞋可以自行查阅。 ;-)

std::lock_guard 介绍

与 Mutex RAII 相关,方便线程对互斥量上锁。例子(参考):

复制代码
#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutex, std::lock_guard#include <stdexcept>      // std::logic_errorstd::mutex mtx;void print_even (int x) {    if (x%2==0) std::cout << x << " is even\n";    else throw (std::logic_error("not even"));}void print_thread_id (int id) {    try {        // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:        std::lock_guard<std::mutex> lck (mtx);        print_even(id);    }    catch (std::logic_error&) {        std::cout << "[exception caught]\n";    }}int main (){    std::thread threads[10];    // spawn 10 threads:    for (int i=0; i<10; ++i)        threads[i] = std::thread(print_thread_id,i+1);    for (auto& th : threads) th.join();    return 0;}
复制代码

std::unique_lock 介绍

与 Mutex RAII 相关,方便线程对互斥量上锁,但提供了更好的上锁和解锁控制。例子(参考):

复制代码
#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutex, std::unique_lockstd::mutex mtx;           // mutex for critical sectionvoid print_block (int n, char c) {    // critical section (exclusive access to std::cout signaled by lifetime of lck):    std::unique_lock<std::mutex> lck (mtx);    for (int i=0; i<n; ++i) {        std::cout << c;    }    std::cout << '\n';}int main (){    std::thread th1 (print_block,50,'*');    std::thread th2 (print_block,50,'$');    th1.join();    th2.join();    return 0;}
复制代码

好了,本文暂时讲到这里,还剩下 std::try_lock,std::lock,std::call_once 三个函数没有讲到,留在下一篇博客中讲吧 ;-)




C++11 并发指南三(Lock 详解)


在 《C++11 并发指南三(std::mutex 详解)》一文中我们主要介绍了 C++11 标准中的互斥量(Mutex),并简单介绍了一下两种锁类型。本节将详细介绍一下 C++11 标准的锁类型。

C++11 标准为我们提供了两种基本的锁类型,分别如下:

  • std::lock_guard,与 Mutex RAII 相关,方便线程对互斥量上锁。
  • std::unique_lock,与 Mutex RAII 相关,方便线程对互斥量上锁,但提供了更好的上锁和解锁控制。

另外还提供了几个与锁类型相关的 Tag 类,分别如下:

  • std::adopt_lock_t,一个空的标记类,定义如下:
struct  adopt_lock_t {};

 该类型的常量对象adopt_lock(adopt_lock 是一个常量对象,定义如下:

constexpr 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值