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
247
针对
t
实施了
detach
。在此情况下,
fut
无须在析构函数中实施
detach
因为在
调用的代码已经做过这件事了。
换句话说,当你的期值所对应的共享状态是由
std::packaged_task
产生的,则通
常无需采用特别析构策略。因为,关于是终止、联结还是分离的决定,会由操纵
std::thread
的代码作出,而
std::packaged_task
通常就运行在该线程之上。
要点速记
期值的析构函数在常规情况下,仅会析构期值的成员变量。
指涉到经由
std::aysnc
启动的未推迟任务的共享状态的最后一个期值会保
持阻塞,直至该任务结束。
条款
39
:考虑针对一次性事件通信使用以
void
为模板型别实参的期值
有时候,提供让一个任务通知另一个以异步方式运行的任务发生了特定的事件的能力,
会是有用的,原因可能是第二个任务在事件发生之前无法推进。这事件也许是某个数
据结构完成初始化了,也许是某个计算阶段结束了,又也许是某个重要传感器取值被
检测到了等。在此情况下,何为线程间通信的最佳方式?
一种明显的途径是使用条件变量。若我们把检测条件的任务称为检测任务,把对条件
作出反应的任务称为反应任务,则策略表述起来很简单:反应任务等待着条件变量,
而检测任务则在事件发生时通知条件变量。给定:
std::condition_variable cv;
//
事件的条件变量
std::mutex m;
//
在运用
cv
时给它加的互斥量
检测任务的代码可谓简单到不能再简单了:
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