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++(中文版)
262
7
volatile std::atomic<int>
vai;
//
针对
vai
的操作是原子的,
//
并且不可以被优化掉
如果
vai
对应于由多个线程同时访问的内存映射
I/O
位置,就可能会是有用的。
最后,有些开发人员更喜欢使用
std::atomic
load
store
成员函数,即使并非必要,
因为这样做可以在源代码中明确地表示所涉及的变量并非“常规”。强调这一事实,
也并非没有理由。访问
std::atomic
型别对象通常比访问非
std::atomic
型别对象慢
得多,我们已经看到
std::atomics
别对象在使用过程中会阻止编译器对某些类型
的代码重新排序,而这样的重新排序在其他情况下是被允许的。召唤
std::atomic
别对象的加载和存储有助于识别出阻碍潜在的可伸缩性之处。从正确性角度来看,如
果本来想要通过某个变量将信息传达到其他线程,却未见它调用
store
(例如,一个指
示数据可用性的标志位),就可能意味着该变量本来应该声明为
std::atomic
,却没有
这样做。
这在很大程度上是一个代码风格问题,因此,这与在
std::atomic
volatile
之间
进行的选择有着非常不同的性质。
要点速记
std::atomic
用于多线程访问的数据,且不用互斥量。它是撰写并发软件的
工具。
volatile
用于读写操作不可以被优化掉的内存。它是在面对特种内存时使
用的工具。
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