Cplusplus-Concurrency-In-Practice Documentation

A Detailed Cplusplus Concurrency Tutorial 《C++ 并发编程指南》

5,496 stars C++ C++ 9 files · ~3,719 tokens
RAW Doc

Technical Documentation: forhappy/Cplusplus-Concurrency-In-Practice

ℹ️ Provenance: Multi-Tier Ingestion · CodeWiki Reference

Overview (`README.md`)

写作动机

本书《C++ 并发编程指南》是个人在空余时间写的,由于时间仓促,加上自身水平有限,不可能写的很完善,也难免出现错误,如果你发现本书中的错误,或者有更好的想法,欢迎给我反馈,我会第一时间给予答复。后续我会坚持完善这一系列的文章。也希望感兴趣的同学和我一起完成。

本书的创作出于以下两个目的:

  • 传播知识,介绍 C++ 并发编程。目前国内还没有一本完整介绍 C++11 并发编程的中文书籍,希望本书可以帮助广大的 C++ 开发者学习并发编程,降低 C++ 并发编程的学习难度,减少初学者对 C++ 并发编程的恐惧感。
  • 自我激励和提高。创作本书的另一个目的是自我激励和提高。和大部分人一样,本人也是 C++ 菜鸟一枚,在创作本书的过程中我会查阅大量资料,时间长了,自己学到的东西就慢慢积累多了。同时,在写作的时候为了表达清楚和准确,个别语句我会不断推敲,对自己的思维和语言表达能力很有帮助的。最后我坚信: 菜鸟都一样(因为什么都不会),牛人各有各的不同。

本书还处于创作早期(2013 年 8 月 31 日提交第一行内容),还有很多内容需要修正和完善,如果你对此感兴趣,希望能加入到我们的队伍中(虽然目前还只有我一人孤身奋战)一起来完成 《C++ 并发编程指南》,传播知识,方便他人,提高自我吧。

本书目录(只列出一级目录)

第一章 并发编程基础

第二章 几种常见的多线程库介绍

第三章 线程详解

第四章 互斥量与锁

第五章 条件变量与线程同步

第六章 异步任务详解

第七章 原子类型详解

第八章 C++11 内存模型

第九章 高级线程管理

第十章 如何编写正确的并发数据结构

第十一章 并发编程应用实例

附录 C++11 新标准概览

本书的详细目录和完成情况请移步《C++ 并发编程指南》目录

本书创作发起人

forhappy(haipingf AT gmail DOT com)

微博: @傅海平ICT(www.weibo.com/1702076100)

贡献者名单

欢迎申请加入该名单 ;-)

License

The MIT License (MIT)

Copyright (c) 2013 傅海平

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Overview (`zh/appendix C++11 standards/README.md`)

本章导读

本章主要介绍 C++11 标准的一些新特性,本文的素材主要来源于:

Bjarne Stroustrup 个人主页:

C++ 标准委员会:

维基百科:

Isocpp.org:

Boostcon:

按照维基百科中的分类方法,本文将对 C++11 标准的新特性按照以下四个方面来解释,分别是:

  1. 核心语言的运行时性能强化
  2. 核心语言的构建时性能强化
  3. 核心语言的可用性强化
  4. 核心语言的功能提升
  5. C++ 标准库的变更

附录 C++11 新标准概览

1. [核心语言的运行时性能强化](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/appendix%20C%2B%2B11%20standards/C%2B%2B11%20Core%20language%20runtime%20performance%20enhancements.md)

本小节主要包括:

  1. 右值引用和 move 语义
  2. 泛化的常量表达式 constexpr
  3. 对 POD 类型定义的修正。

2. [核心语言的构建时性能强化](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/appendix%20C%2B%2B11%20standards/C%2B%2B11%20Core%20language%20build%20time%20performance%20enhancements.md)

本小节主要包括:

  1. 外部模板。

3. [核心语言的可用性强化](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/appendix%20C%2B%2B11%20standards/C%2B%2B11%20Core%20language%20usability%20enhancements.md)

本小节主要包括:

  1. 初始化列表(std::initializer_list)
  2. 统一的初始化方式
  3. 类型推导(auto 和 decltype 关键字)
  4. 基于范围的 for 循环
  5. lambda 表达式
  6. 另一种可选的函数语法
  7. 对象创建优化
  8. 显式虚函数重载
  9. 空指针常量(nullptr)
  10. 强类型枚举
  11. 右尖括号(>)
  12. 显式类型转换操作符
  13. 模板别名
  14. 无限制 union。

4. 核心语言的功能提升

本小节主要包括:

  1. 变长参数模板
  2. 新的字符串字面值
  3. 用户自定义的字面值
  4. 多线程内存模型
  5. 线程本地存储
  6. 显式地使用或禁用某些特殊成员函数(构造函数,拷贝构造,赋值操作符,析构等)
  7. long long int类型
  8. 静态断言 assertions
  9. 允许 sizeof 运算符作用在类型的数据成员上,无须明确的对象
  10. 垃圾回收机制
  11. 属性

5. C++ 标准库的变更

本小节主要包括:

  1. 标准库组件上的升级
  2. 多线程支持
  3. 元组(tuple)类型
  4. 散列表(hash table)
  5. 正则表达式
  6. 通用智能指针
  7. 可扩展的随机数功能
  8. 包装引用
  9. 多态函数对象包装器
  10. 用于元编程的类型属性
  11. 用于计算函数对象返回类型的统一方法

Overview (`zh/chapter1-Introduction/README.md`)

本章导读

本章主要内容如下:

  • 什么是并发编程,并发和并行的联系和区别,为什么需要并发编程。
  • 并发编程应用场景,并提出几个并发编程的经典示例。
  • C++ 并发编程初探。

我会在本章介绍一些最基本的概念,关键词如:程序,并发,并行,进程,多线程,进程间通信,线程间数据共享问题,通过对本章内容的学习,你会对并发编程有一个大致的认识,究竟什么是并发编程,并发和并行的联系和区别,什么场景下需要使用并发编程以及并发的优势和缺陷等。对于初学者,我会给出几个经典的示例激发大家的兴趣。

第一章 并发编程基础

1.1 [什么是并发编程](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter1-Introduction/1.1%20What%20is%20concurrency.md)

1.2 [并发与并行的区别和联系](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter1-Introduction/1.1%20What%20is%20concurrency.md#12-%E5%B9%B6%E5%8F%91%E4%B8%8E%E5%B9%B6%E8%A1%8C%E7%9A%84%E8%81%94%E7%B3%BB%E5%92%8C%E5%8C%BA%E5%88%AB)

1.3 为什么需要并发编程

1.4 并发编程应用场景和经典示例

1.5 [C++ 并发编程初探](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter1-Introduction/Cplusplus-Concurrency-Introduction.md)

1.6 [资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter1-Introduction/web-resources.md)


Overview (`zh/chapter3-Thread/README.md`)

本章导读

本章主要内容如下:

  • `` 头文件中的类及相关函数的介绍,包括创建新的线程对象、线程对象的控制和管理、与线程相关的异常处理等。
  • std::thread 与 Pthread 等线程库的对比,包括两者编程接口的差异及其他细节。
  • std::thread 应用实例。

第三章 线程详解

[3.1 `` 头文件摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#thread-%E5%A4%B4%E6%96%87%E4%BB%B6%E6%91%98%E8%A6%81)

[3.1.1 `std::thread` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E7%B1%BB%E6%91%98%E8%A6%81)

[3.2 `std::thread` 详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E8%AF%A6%E8%A7%A3)

[3.2.1 `std::thread` 构造和赋值](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E6%9E%84%E9%80%A0%E5%92%8C%E8%B5%8B%E5%80%BC)

[3.2.1.1 `std::thread` 构造函数](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0)

[3.2.2.2 `std::thread` 赋值操作](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E8%B5%8B%E5%80%BC%E6%93%8D%E4%BD%9C)

[3.2.2 其他成员函数](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#%E5%85%B6%E4%BB%96%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0)

[3.3 `std::this_thread` 命名空间中相关辅助函数介绍](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthis_thread-%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E4%B8%AD%E7%9B%B8%E5%85%B3%E8%BE%85%E5%8A%A9%E5%87%BD%E6%95%B0%E4%BB%8B%E7%BB%8D)

3.4 `std::thread` 与 Pthread 对比

[3.5 资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter3-Thread/web-resources.md "资料汇")


Overview (`zh/chapter4-Mutex/README.md`)

本章导读

本章主要内容如下:

  • <mutex> 头文件中的类及相关函数的介绍,包括互斥量、各种锁类型的介绍,以及 lock, try_lock,call_once 等函数的用法。
  • std::mutex 与 Pthread 等线程库中的 pthread_mutex_t 类型的对比,包括两者编程接口的差异及其他细节。
  • std::mutex 和 std::lock_guard、std::unique_lock 应用实例。

第四章 互斥量与锁

[4.1 `` 头文件摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md)

[4.1.1 `std::mutex` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#411-stdmutex-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.1.2 `std::recursive_mutex` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#412-stdrecursive_mutex-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.1.3 `std::timed_mutex` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#413-stdtimed_mutex-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.1.4 `std::recursive_timed_mutex` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#414-stdrecursive_timed_mutex-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.1.5 `std::lock_guard` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#415-stdlock_guard-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.1.6 `std::unique_lock` 类摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.1%20Mutex-header-synopsis.md#416-stdunique_lock-%E7%B1%BB%E6%91%98%E8%A6%81)

[4.2 互斥量详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.2%20Mutex-tutorial.md)

4.2.1 `std::mutex` 类型介绍

4.2.1.1 `std::mutex` 构造函数

4.2.1.2 `std::mutex` 赋值操作

4.2.1.3 其他成员函数

4.2.2 `std::recursive_mutex` 类型介绍

4.2.3 `std::timed_mutex` 类型介绍

4.2.4 `std::recursive_timed_mutex` 类型介绍

[4.3 锁类型详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/4.3%20Lock-tutorial.md)

4.3.1 `std::lock_guard` 类型介绍

4.3.2 `std::unique_lock` 类型介绍

4.4 辅助函数介绍

4.5 `std::mutex` 与 Pthread 对比

[4.6 资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter4-Mutex/web-resources.md)


Overview (`zh/chapter5-Condition-Variable/README.md`)

本章导读

本章主要内容如下:

  • <condition_variable> 头文件中的类及相关函数的介绍。
  • std::condition_variable 与 Pthread 线程库中的 pthread_cond_t 类型的对比,包括两者编程接口的差异及其他细节。
  • 利用条件变量 std::condition_variable 进行线程同步的应用实例。

第五章 条件变量与线程同步

5.1 [`` 头文件摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter5-Condition-Variable/5.1%20Condition-variable%20header%20synopsis.md)

[5.2 条件变量详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter5-Condition-Variable/5.2%20Condition-variable-tutorial.md)

[5.3 辅助函数介绍](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter5-Condition-Variable/5.3%20Auxiliary-function.md)

5.4 利用条件变量(std::condition_variable)进行线程同步

5.5 `std::condition_variable` 与 Pthread 条件变量对比

[5.6 资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter5-Condition-Variable/web-resources.md)


Overview (`zh/chapter6-Future/README.md`)

本章导读

本章主要内容如下:

  • 异步任务相关的概念以及 C++11 标准 <future> 头文件中的类及相关函数的介绍。
  • C++11 标准的异步任务基础设施对多线程程序设计带来的影响。
  • 异步任务实例。

第六章 异步任务详解

6.1 [`` 头文件摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.1%20Future-header-synopsis.md)

6.2 [异步任务提供者(Provider) 介绍](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.2%20Providers-tutorial.md)

6.3 [异步任务提供者(Provider) 介绍(续)](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.3%20Providers-tutorial-2.md)

6.4 [异步任务 Future 类型详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.4%20Future-tutorial.md)

6.5 [与异步任务相关的类型介绍](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.5%20Auxiliary-types.md)

6.6 [异步任务辅助函数 `std::async` 介绍](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/6.6%20Auxiliary-function.md)

6.7 异步任务与多线程实例

6.8 [资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter6-Future/web-resources.md)


Overview (`zh/chapter7-Atomic/README.md`)

本章导读

本章主要内容如下:

  • 什么是原子类型,原子类型的适用场景。
  • C++11 标准中 <atomic> 头文件中的类及相关函数的介绍。
  • 利用原子变量 std::atomic 进行线程同步的应用实例。

第七章 原子类型详解

7.1 [`` 头文件摘要](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/7.1%20Atomic-header-synopsis.md)

7.2 [`std::atomic_flag` 详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/7.2%20Atomic-flag-tutorial.md)

7.3 [基本 `std::atomic` 类型详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/7.3%20Atomic-tutorial.md)

7.4 [特化的 `std::atomic` 类型详解](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/7.4%20Atomic-tutorial2.md)

7.5 [C 风格的原子操作](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/7.5%20C-style-atomic.md)

7.6 C++11 内存模型初探

7.7 如何利用原子类型设计并发数据结构

7.8 [资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter7-Atomic/web-resources.md)


Overview (`zh/chapter8-Memory-Model/README.md`)

本章导读

本章主要介绍 C++11 的内存模型,并结合前一章介绍的原子类型给出相应的实例。主要内容包括:

  • C++ 内存模型概述
  • X86 CPU 处理器架构与存储一致性模型简介
  • 内存序(Memory Order)与同步操作
  • 原子类型操作中内存序(Memory Order)参数的选取与编程实例
  • Lock-free 编程初探

8.1 C++ 内存模型概述

8.2 X86 CPU 处理器架构与常见的存储一致性模型简介

8.3 内存序(Memory Order)与同步操作

8.4 原子类型编程实例

8.5 Lock-free 编程初探

[资料汇](https://github.com/forhappy/A-Detailed-Cplusplus-Concurrency-Tutorial/blob/master/zh/chapter8-Memory-Model/web-resources.md)


--- METRICS ---