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++(中文版)
并发
API
243
条款
38
:对变化多端的线程句柄析构函数行为保
持关注
条款
37
解释过,可联结的线程对应着一个底层系统执行线程,未推迟任务(参见
36
)的期值和系统线程也有类似关系。这么一来,
std::thread
别对象和期值对
象都可以视作系统线程的句柄。
从这个视角来看,
std::thread
对象和期值对象的析构函数表现出如此不同的行为值
得深思。正如条款
37
所提及的,针对可联结的
std::thread
型别对象实施析构会导
致程序终止,因为另外两个显而易见的选择(隐式
join
和隐式
detach
)都被认为是
更糟糕的选择。而期值的析构函数呢,有时候行为像是执行了一次隐式
join
,有时候
行为像是执行了一次隐式
detach
,有时候行为像是二者都没有执行。它从不会导致程
序终止。这套线程句柄行为的大杂烩,值得我们仔细品鉴一番。
我们的观察从这里开始:期值位于信道的一端,被调方把结果通过该信道传输给调用
方。
4
被调方(通常以异步方式运行)把其计算所得的结果写入信道(通常经由一
std::promise
型别对象),而调用方则使用一个期值来读取该结果。你可以把这
个过程想像成下图,虚线箭头代表着从被调方向调用方的信息流:
调用方 被调方
期值
(一般情况)
但被调方的结果要存储在哪里呢?在调用方唤起对应期值的
get
之前,被调方可能已
经执行完毕,因此结果不会存储在被调方的
std::promise
别对象里。那个对象,对
于被调方来说是个局部量,在被调方结束后会实施析构。
该结果也不能存储在调用方的期值中,因为
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

C++程序设计:原理与实践(基础篇)(原书第2版)

C++程序设计:原理与实践(基础篇)(原书第2版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749