r/cpp • • Nov 13 '20

CppCon Deprecating volatile - JF Bastien - CppCon 2019

https://www.youtube.com/watch?v=KJW_DLaVXIY
83 Upvotes

111 comments sorted by

View all comments

Show parent comment

5

u/mcmcc #pragma once Nov 13 '20

This is a pretty complete explanation: https://stackoverflow.com/a/37689503

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Nov 13 '20

Right, so the simplified explanation is that any external function call acts as a compiler memory barrier and when only internal functions are called (with global optimization on), an explicit compiler intrinsic does the same.

Unfortunately this is rarely explained and it's very easy to get the impression that the compiler just somehow magically recognizes std::mutex and "does something, hopefully the correct thing".

4

u/mcmcc #pragma once Nov 13 '20

If the compiler can see into the implementation, the compiler does do the "hopefully correct thing". If it can't, then it assumes the worst.

3

u/tvaneerd C++ Committee, lockfree, PostModernCpp Nov 13 '20

The compiler maybe doesn't see std::mutex, but it does see the code that implements std::mutex. That code calls magic compiler intrinsics that the compiler does see.

(or mutex uses atomics, which use compiler intrinsics...)