Effective Modern C++ 笔记 第七章 The Concurrency API

本文探讨了C++中任务和线程的使用建议,包括std::async和std::thread的最佳实践,以及std::atomic和volatile关键字的区别。文章还讨论了如何避免一些常见的陷阱,如std::thread析构行为的变化。
Item 35: Prefer task-based programming to thread-based. 241
Item 36: Specify std::launch::async if asynchronicity is essential. 245
Item 37: Make std::threads unjoinable on all paths. 250
Item 38: Be aware of varying thread handle destructor behavior. 258
Item 39: Consider void futures for one-shot event communication. 262

Item 40: Use std::atomic for concurrency, volatile for special memory.


这章开始讲STL了,理解起来比core language简单,需要的经验值比core language更高。。。讲core language尚可以像教科书般的细细研读,讲STL的话,基本就是看得懂,不知道为什么,看完就忘。。。


从std::thread线程语义进化到std::async的task语义,使用起来更方便,然后就是坑又多了几个,比如关于std::async的policy,比如关于std::future其实我有shared state等等。然后顺便提了std::thread的坑:如果thread是joinable的,那么std::thread的dtor直接结束程序。所以在dtor之前,一定要留意吧程序的thread对象都置为unjoinable。


Item39介绍了一种通过future,promise来替代mutex,cond_var组合的通知形式,不过只适用于事件被通知一次的情况。而mutex,cond_var组合是可以通知多次的。被pthread_mutex, pthread_cond 虐过的看个条款很轻松。


Item40介绍了atomic和很苦逼的volatile关键字。atomic印象最深的就是copy ctor和operator=是被delete的。这个Item的扩展可以看下Herb Sutter的视频: C++ Atomic Weapons,channel9有下



Coming to grips with C++11 and C++14 is more than a matter of familiarizing yourself with the features they introduce (e.g., auto type declarations, move semantics, lambda expressions, and concurrency support). The challenge is learning to use those features effectively—so that your software is correct, efficient, maintainable, and portable. That’s where this practical book comes in. It describes how to write truly great software using C++11 and C++14—i.e. using modern C++. Topics include: The pros and cons of braced initialization, noexcept specifications, perfect forwarding, and smart pointer make functions The relationships among std::move, std::forward, rvalue references, and universal references Techniques for writing clear, correct, effective lambda expressions How std::atomic differs from volatile, how each should be used, and how they relate to C++'s concurrency API How best practices in "old" C++ programming (i.e., C++98) require revision for software development in modern C++ Effective Modern C++ follows the proven guideline-based, example-driven format of Scott Meyers' earlier books, but covers entirely new material. "After I learned the C++ basics, I then learned how to use C++ in production code from Meyer's series of Effective C++ books. Effective Modern C++ is the most important how-to book for advice on key guidelines, styles, and idioms to use modern C++ effectively and well. Don't own it yet? Buy this one. Now". -- Herb Sutter, Chair of ISO C++ Standards Committee and C++ Software Architect at Microsoft
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值