C++核心準(zhǔn)則CP.200:使用volatile只能表明該變量是非C++內(nèi)存

CP.200: Use?volatile?only to talk to non-C++ memory
CP.200:使用volatile只能表明該變量是非C++內(nèi)存
Reason(原因)
volatile?is used to refer to objects that are shared with "non-C++" code or hardware that does not follow the C++ memory model.
volatile用于表明參照的對象需要和非C++代碼或硬件共享而遵守C++內(nèi)存模型。
Example(示例)
const volatile long clock;
This describes a register constantly updated by a clock circuit.?clock?is?volatile?because its value will change without any action from the C++ program that uses it. For example, reading?clock?twice will often yield two different values, so the optimizer had better not optimize away the second read in this code:
這段代碼描述一個不斷被時鐘電路更新的寄存器。clock被定義為volatile是因為它的值在使用它的C++程序沒有任何動作的情況下被修改。例如,兩次讀取clock經(jīng)常可以得到不同的值,因此優(yōu)化器最好不要優(yōu)化掉這段代碼中的第二個讀操作。
long t1 = clock;
// ... no use of clock here ...
long t2 = clock;
clock?is?const?because the program should not try to write to?clock.
clock定義為常量是為了表明程序不應(yīng)該對clock進(jìn)行寫操作。
Note(注意)
Unless you are writing the lowest level code manipulating hardware directly, consider?volatile?an esoteric feature that is best avoided.
除非你正在編寫直接操作硬件的低層次代碼,否則將volatile作為冷門功能并最好避免使用。
Example(示例)
Usually C++ code receives?volatile?memory that is owned elsewhere (hardware or another language):
通常情況下,C++代碼接受有其他某處擁有的volatile內(nèi)存(硬件或其他語言):
int volatile* vi = get_hardware_memory_location();
// note: we get a pointer to someone else's memory here
// volatile says "treat this with extra respect"
Sometimes C++ code allocates the?volatile?memory and shares it with "elsewhere" (hardware or another language) by deliberately escaping a pointer:
某些C++代碼會分配volatile內(nèi)存并通過故意泄露指針的方式和其他部分共享(硬件或其他語言)它。
static volatile long vl;
please_use_this(&vl); // escape a reference to this to "elsewhere" (not C++)Example, bad(反面示例)
volatile?local variables are nearly always wrong -- how can they be shared with other languages or hardware if they're ephemeral? The same applies almost as strongly to member variables, for the same reason.
volatile類型的局部變量幾乎一定是錯的--如果它們只能短期存在,怎么能分享給其他語言或硬件呢?由于同樣的原因,該原則也幾乎一定適用于成員變量。
void f()
{
volatile int i = 0; // bad, volatile local variable
// etc.
}
class My_type {
volatile int i = 0; // suspicious, volatile member variable
// etc.
};Note(注意)
In C++, unlike in some other languages,?volatile?has?nothing to do with synchronization.
和其他語言不同,在C++中不會為同步做任何事情。
Enforcement(實施建議)
Flag?volatile T?local and member variables; almost certainly you intended to use?atomic
?instead. 標(biāo)記volatile類型的局部變量和成員變量;幾乎可以肯定的說你想用的其實是atomatic
。 ???
原文鏈接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp200-use-volatile-only-to-talk-to-non-c-memory
新書介紹
以下是本人3月份出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。
對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
