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++(中文版)
242
7
哎呀,条款
39
会展示,使用
ThreadRAII
std::thread
析构中实施
join
是仅仅会
导致性能异常那么简单,而是会导致程序失去响应。这种问题的“合适的”解决方案
是和异步执行的
lambda
式通信,当我们已经不再需要它运行,它应该提前返回,但
C++11
中并不支持这种可中断线程。我们可以手动实现它,但这个话题超出了本书的
范围。
3
条款
17
解释过,因为
ThreadRAII
声明了析构函数,所以不会有编译器生成的移动操
作,但这里
ThreadRAII
对象没有理由实现为不可移动的。如果编译器会生成这些函数,
这些函数的行为就是正确的,所以显式地请求创建它们是适当的:
class ThreadRAII {
public:
enum class DtorAction { join, detach };
//
同前
ThreadRAII(std::thread&& t, DtorAction a)
//
同前
: action(a), t(std::move(t)) {}
~ThreadRAII()
{
//
同前
}
ThreadRAII(ThreadRAII&&) = default;
//
支持
ThreadRAII& operator=(ThreadRAII&&) = default;
//
移动操作
std::thread& get() { return t; }
//
同前
private:
//
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