Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
254
7
std::vector<std::thread> vt;
//
反应任务的容器
for (int i = 0; i < threadsToRun; ++i) {
vt.emplace_back([
sf
]{ sf.wait();
// sf
局部副本之上的
wait
react(); });
//
关于
emplace_back
,参见条款
42
}
//
若此“…”抛出异常,则
detect
会失去响应!
p.set_value();
//
让所有线程取消暂停
for (auto& t : vt) {
//
把所有线程置为不可联结状态
t.join();
//
欲知“
auto&
”详情,参见条款
2
}
}
使用期值的设计能够实现这样的效果,此事实值得注意,这也是为何应该将一次性事
件通信纳入考量。
要点速记
如果仅为了实现平凡事件通信,基于条件变量的设计会要求多余的互斥量,
这会给相互关联的检测和反应任务带来约束,并要求反应任务校验事件确
已发生。
使用标志位的设计可以避免上述问题,但这一设计基于轮询而非阻塞。
条件变量和标志位可以一起使用,但这样的通信机制设计结果不甚自然。
使用
std::promise
型别对象和期值就可以回避这些问题,但是一来这个途
径为了共享状态需要使用堆内存,而且仅限于一次性通信。
条款
40
:对并发使用
std::atomic
,对特种内存
使用
volatile
可怜的
volatile ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Effective STL

Effective STL

Scott Meyers

Publisher Resources

ISBN: 9787519817749