GithubHelp home page GithubHelp logo

downdemo / cpp-concurrency-in-action-2ed Goto Github PK

View Code? Open in Web Editor NEW
1.9K 20.0 323.0 623 KB

C++11/14/17/20 multithreading, involving operating system principles and concurrent programming technology.

Home Page: https://downdemo.github.io/Cpp-Concurrency-in-Action-2ed/

License: Apache License 2.0

C++ 100.00%
operating-systems multithreading

cpp-concurrency-in-action-2ed's Introduction

  1. 线程管理(Managing thread)<thread>
  2. 线程间共享数据(Sharing data between thread)<mutex><shared_mutex>
  3. 同步并发操作(Synchronizing concurrent operation)<condition_variable><semaphore><barrier><latch><future><chrono><ratio>
  4. C++ 内存模型和基于原子类型的操作(The C++ memory model and operations on atomic type)<atomic>

并发编程实践

  1. 基于锁的并发数据结构的设计(Designing lock-based concurrent data structure)
  2. 无锁并发数据结构的设计(Designing lock-free concurrent data structure)
  3. 并发代码的设计(Designing concurrent code)
  4. 高级线程管理(Advanced thread management)
  5. 并行算法(Parallel algorithm)<execution>
  6. 多线程应用的测试与调试(Testing and debugging multithreaded application)

标准库相关头文件

头文件 说明
<thread><stop_token> 线程
<mutex><shared_mutex>
<condition_variable> 条件变量
<semaphore> 信号量
<barrier><latch> 屏障
<future> 异步处理的结果
<chrono> 时钟
<ratio> 编译期有理数算数
<atomic> 原子类型和原子操作
<execution> 标准库算法执行策略

并发库对比

特性 API
thread std::thread
mutex std::mutexstd::lock_guardstd::unique_lock
condition variable std::condition_variablestd::condition_variable_any
atomic std::atomicstd::atomic_thread_fence
future std::futurestd::shared_future
interruption
特性 API
thread boost::thread
mutex boost::mutexboost::lock_guardboost::unique_lock
condition variable boost::condition_variableboost::condition_variable_any
atomic
future boost::futureboost::shared_future
interruption thread::interrupt
特性 API
thread pthread_createpthread_detachpthread_join
mutex pthread_mutex_lock、pthread_mutex_unlock
condition variable pthread_cond_waitpthread_cond_signal
atomic
future
interruption pthread_cancel
特性 API
thread java.lang.Thread
mutex synchronized blocks
condition variable java.lang.Object.waitjava.lang.Object.notify
atomic volatile 变量、java.util.concurrent.atomic
future java.util.concurrent.Future
interruption java.lang.Thread.interrupt
线程安全的容器 java.util.concurrent 中的容器
线程池 java.util.concurrent.ThreadPoolExecutor

cpp-concurrency-in-action-2ed's People

Contributors

downdemo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp-concurrency-in-action-2ed's Issues

在“8.1.2 以递归方式划分数据”中的 parallel_quick_sort不能正常运行

下面是运行代码:
int main(){
std::list data = {5,1,2,9,100,8};
auto sorted_data = parallel_quick_sort(data);
for(auto it: sorted_data ){
printf("%d", it);
}
}
出错信息:
terminate called after throwing an instance of 'EmptyStack'
what(): empty stack!
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-m25ftpou.0wk" 1>"/tmp/Microsoft-MIEngine-Out-xpupxdid.5th"

关于线程池这一章的问题

“往线程池添加任务会增加任务队列的竞争,lock-free 队列可以避免这点但存在乒乓缓存的问题。为此需要把任务队列拆分为线程独立的本地队列和全局队列,当线程队列无任务时就去全局队列取任务”

在这一小节中,submit函数中的local_queue永远是nullptr,原因如下:
local_queue指针在worker_thread中赋值,woker_thread运行在子线程,而submit函数是在main线程中调用的,二者不是在同一线程,所以二者的local_queue根本不是同一个。

所以这段代码无法达到想要的目的。

some questions.

你好,我最近也在看这本书,我有一些疑问想跟你探讨一下不知你是否方便?

  1. P20 ,The act of calling join() also cleans up any storage associated with the thread, so the std::thread object is no longer associated with the now-finished thread; it isn’t associated with any thread. 这句话我没太懂?

  2. P22 The copy constructor and copy-assignment operators are marked =delete to ensure
    that they’re not automatically provided by the compiler. Copying or assigning such an
    object would be dangerous, because it might then outlive the scope of the thread it was
    joining. By declaring them as deleted, any attempt to copy a thread_guard object will
    generate a compilation error. 如果不将copy-assignment 定为 deleted的话,为什么是危险的呀?能否举一个例子?

应该是C++11规定 static local variable 的初始化是线程安全的和 copy assignment operator 漏写了字符 =

downdemo1

文件位置: content/02 线程间共享数据.md
https://github.com/downdemo/Cpp-Concurrency-in-Action-2ed/blob/cbd88caed525fffb736b4fe2fd3aa61a2d1f3bb2/content/02%20%E7%BA%BF%E7%A8%8B%E9%97%B4%E5%85%B1%E4%BA%AB%E6%95%B0%E6%8D%AE.md

Issue1

《C++ Concurrency in Action》Second Edition,Chapter 3.3.1 节最后部分关于 static变量 初始化的描述,仅指的是 static local variable 的初始化自 C++11 起是线程安全的,对于 static non-local variable 是未提及的,即对于具有静态存储期的非局部变量,如 global variable 和类的 static data member ,它们的初始化会作为程序启动的一部分在 main 函数的执行之前进行:简单的类型会在编译期进行初始化,如 constexpr 类型,复杂的类型则需要在运行期初始化,如 复杂的类对象(初始化时需要调用构造函数)。在程序结束时它们才会被销毁。因为 static non-local variable 的初始化会作为程序启动的一部分在 main 函数的执行之前进行,所以其初始化不存在多个线程竞争初始化的问题,但是却存在异常安全和 initialization order 的问题。详见 Static local variable and Global variable

即文中应该写为: “C++11规定static局部变量的初始化……”

Issue2

class A 的 copy assignment operator 漏写了字符 = ,即应该是: A& operator=(const A&) = delete;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.