Skip to Content
C++语言导学(原书第2版)
book

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

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

15.1 引言

并发,也就是多个任务同时执行,被广泛用于提高吞吐率(用多个处理器共同完成单个运算)和提高响应速度(允许程序的一部分在等待响应时,另一部分继续执行)。所有的现代程序设计语言都提供了对并发的支持。C++标准库并发设施的前身在C++中已应用超过20年了,经过对可移植性和类型安全的改进,成为标准库的一部分,它几乎适用于所有现代硬件平台。标准库并发设施重点提供系统级并发机制,而不是直接提供复杂的高层并发模型。基于标准库并发设施,可以构建出提供这类高层并发模型的库。

标准库直接支持在单一地址空间内并发执行多个线程。为此,C++提供了一个适合的内存模型和一套原子操作。原子操作可实现无锁的并发程序设计[Dchev,2010],内存模型则保证了:只要程序员避免了数据竞争(对可变数据的不受控制的并发访问),程序运行结果就是可预料的。但是,大多数用户眼中的并发就是标准库设施以及建立在其上的其他并发库。因此,本章简要介绍主要的标准库并发设施——thread、mutex、lock()操作、packaged_task和future,给出一些示例。这些特性直接建立在操作系统并发机制之上,与系统原始机制相比,它们并不会带来额外的性能开销,当然也不保证有显著性能提升。

不要将并发看作万能灵药。如果串行执行已经能很好地完成一个任务,那么使用串行程序就好了,这通常更简单也会更快。

显式使用并发特性的一个替代选择是并行算法,我们通常可以使用并行算法来利用多个执行单元提高性能(参见12.9节、14.3.1节)。

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

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280