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++(中文版)
228
7
并发
API
C++11
的至伟功勋之一,就是将并发融入了语言和库中。熟悉其他线程
API
(例如,
pthread
Windows
线程库)的程序员有时会对
C++
提供的相对斯巴达式地简练的特
性集感觉惊讶,但这是因为,
C++
对并发的支持中有一大部分是以对编译器厂商实施
约束的形式提供的。由此得到的一系列语言层面的保障,意味着在
C++
的历史上,程
序员首次可以跨越所有平台撰写具有标准行为的多线程程序。这就为建立富有表达力
的库奠定了坚实的基础,而标准库中的并发元素(任务、期值、线程、互斥量、条件
变量和原子对象等)仅仅是个开端,今后在开发并行
C++
软件时,肯定会有一套越来
越丰富的工具集。
在本章随后的条款中请牢记,标准库中为期值准备了两个模板:
std::future
std::shared_future
。在很多情况下,它们之间的区别并不重要,所以我经常仅仅谈
论期值概念,意思是指可以对这两种都适用。
条款
35
:优先选用基于任务而非基于线程的程序
设计
如果你想以异步方式运行函数
doAsyncWork
,有两种基本选择。你可以创建一个
std::thread
,并在其上运行
doAsyncWork
,因此这是基于线程(
thread-based
)的途径:
int doAsyncWork();
std::thread
t(doAsyncWork);
抑或,你可以把
doAsynWork
传递给
std::async
,这种策略叫做基于任务(
task-based
):
并发
API
229
auto fut =
std::async ...
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