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

后端 未结 8 1561
旧时难觅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:25

    Your question is technically known as 'a can of worms'! For c/c++ ( I can't comment on java )
    You can very roughly summarise volatile as being a directive to the compiler to say 'please don't optimise this out' but there is a lot of argument amongst professionals, as to whether it's
    a) At all useful for kernel level code <-Edit clarified based on feedback
    b) Even implemented correctly by most compilers.

    Also, do not ever use it for multi-threaded programming and here's a very good explanation as to why

    =Edit= Interestingly, for what it's worth. Dennis Ritchie was against it's inclusion ( as well as const ) details here

提交回复
热议问题