Usage of volatile specifier in C/C++/Java

后端 未结 8 1557
旧时难觅i
旧时难觅i 2021-01-03 01:35

While going through many resources on multithreaded programming, reference to volatile specifier usually comes up. It is clear that usage of this keyword is not a reliable w

相关标签:
8条回答
  • 2021-01-03 02:35

    I found this DDJ article by Herb Sutter very interesting, especially how volatile is treated in C++, Java and C# .NET.

    Dr.Dobbs volatile vs. volatile

    0 讨论(0)
  • Volatile variable should be used when that variable can be accessed by many threads and you want at every instruction your code should get the updated value of that variable.

    Compilers generally optimize the code and store variables in register rather than getting from memory each and every time if they see no one has updated it.

    But by using volatile you can force the compiler to get the updated value every time.

    0 讨论(0)
提交回复
热议问题