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++(中文版)
232
7
你需要实现超越
C++
并发
API
的线程技术
。例如,在
C++
实现中未提供的线程
池的平台上实现线程池。
无论如何,这些都是不常见的情况。大多数时候,你应该选择基于任务的设计,而非
直接进行线程相关的程序设计。
要点速记
std::thread
API
未提供直接获取异步运行函数返回值的途径,而且如
果那些函数抛出异常,程序就会终止。
基于线程的程序设计要求手动管理线程耗尽、超订、负载均衡,以及新平
台适配。
经由应用了默认启动策略的
std::async
进行基于任务的程序设计,大部分
这类问题都能找到解决之道。
条款
36
:如果异步是必要的,则指定
std::launch::async
当调用
std::async
来执行一个函数(或可调用对象)时,你基本上都会想让函数以
异步方式运行。但仅仅通过
std::async
来运行,你实际上要求的并非一定会达成异
步运行的结果,你要求的仅仅是让该函数以符合
std::async
的启动策略来运行。标准
策略有二,它们都是用限定作用域的枚举型别
std::launch
的枚举量(关于限定作
用域的枚举,参见条款
10
)来表示的。假设函数
f
要传递给
std::async
执行,则:
std::launch::async
启动策略意味着函数
f
必须以异步方式运行,亦即,在另
一线程之上执行。
std::launch::deferred
启动策略意味函数
f
只会在
std::async
所返回的期值的
get
wait
得到调用时才运行。
2
亦即,执行会推迟至其中一个调用发生的时刻。 ...
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
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749