Skip to Content
C++程序设计语言(第4部分:标准库)(原书第4版)
book

C++程序设计语言(第4部分:标准库)(原书第4版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
6h 57m
Chinese
Pearson
Content preview from C++程序设计语言(第4部分:标准库)(原书第4版)

41.1 引言

并发,即多个任务同时执行,广泛用于提高吞吐率(使用多个处理器完成单个运算)或提高响应能力(当程序的一部分等待响应时允许另一部分继续执行)。

我们在5.3节中已经介绍了C++标准对并发的支持,当然只是一种导览的形式,本章和下一章将提供更加细致、更加系统化的介绍。

如果一项活动可能与其他活动并发执行,我们就称之为任务(task)。线程(thread)是执行任务的计算机特性在系统层面的表示。一个标准库thread(见42.2节)可执行一个任务。一个线程可与其他线程共享地址空间。即,在单一地址空间中的所有线程能访问相同的内存位置。而并发系统程序员所面临的重要挑战之一就是,确保多线程并发访问内存的方式是合理的。

标准库对并发的支持包括:

·内存模型(memory model):这是对内存并发访问的一组保证(见41.2节),主要是确保简单的普通访问能按人们的朴素预期工作;

·对无锁编程(programming without locks)的支持:这是一些避免数据竞争的细粒度底层机制(见41.3节);

·一个线程(thread)库:这是一组支持传统线程-锁风格的系统级并发编程的组件,如thread、condition_variable和mutex(见42.2节);

·一个任务(task)支持库:这是一些支持任务级并发编程的特性:future、promise、packaged_task和async()(见42.4节)。

这些主题是按照从最基础、最底层到最高层的顺序排列的。内存模型是所有编程风格所共用的。为提高程序员开发效率、尽量减少错误,应在尽可能高的层次上编程。例如,应优先选择future而不是mutex实现信息交换;除非是简单的计数器,否则应优选mutex而不是atomic;诸如此类。尽量将复杂任务留给标准库实现者。 ...

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

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)
可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
C语言核心技术(原书第2版)

C语言核心技术(原书第2版)

Peter Prinz, Tony Crawford
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111544395