The reason this doesn’t occur (according to him) is that the compiler has to treat function calls like a black box and is unable to optimize accesses or re-order around it,
That's one reason, certainly.
Another reason that it might not occur specifically with the GNU toolchain is that GNU's link-time optimization applies only to functions that are compiled with the -flto
option, and therefore carry with them additional information on which the optimization relies. All one needs to prevent GCC's link-time optimization from messing with pthread_mutex_lock()
is to have a version of that function that does not carry the LTO information. (This ordinarily would be the responsibility of the system, not of the application developer.)
if you turn on something like link-time optimization, gcc -flto, you may find that your locks suddenly stop working.
Possibly. And if you do see that, then it constitutes a flaw in your C implementation, whether you attribute it to compiler, linker, library, or all of the above. This is not to say that you should discount the possibility, but rather that it is something that people working on these tools are out to avoid and / or fix, so the likelihood of running into such an issue goes down over time.
I am left wondering how this can be true, when the compiler is able to optimize things like memcpy
to a single instruction, effectively looking across module boundaries without link-time optimization. Could it not then do the same to something like pthread_mutex_lock()
and optimize accesses around it?
What you're talking about now is optimization of specific functions. For example, the compiler knows all about memcpy()
in particular, based on its specifications, and in some cases based on being part of the same, integrated C implementation. It can optimize (say) some memcpy
calls at compile time because it knows what that function is supposed to do, and it recognizes usage idioms that it can optimize without actually looking into the library at all.
A compiler could, in principle, do the same with pthread_mutex_lock()
, but this would not be a problem because such specific-function optimizations are aware of (and rely on) the semantics of the function involved. There's no reason to think that such an optimzation of pthread_mutex_lock()
would fail to preserve that function's well-documented memory-order semantics.
If this is true and the compiler can peer into the pthread_mutex_lock()
function and sees it does not alter a certain variable, so it optimizes accesses, could this affect correctness,
Can compilers have bugs? Yes, they can and do.
Do current versions of any C compilers have a specific bug along those lines? I don't know.
and if so how can such a core function be left so vulnerable to the possibility of not working correctly?
Nobody is leaving functions open to such failures. To the extent that opportunities for such incorrect optimizations exist, "people" are interested in and motivated to fix their compilers, linkers, and libraries to close those holes.
Understand also that it is common for new optimizations to be tested carefully for an extended period -- sometimes years and multiple compiler versions -- before being designated safe for production use. If ever they are.
Does this mean there has to be some other method employed to tell the compiler not to optimize accesses to these variables, such as the volatile
construct?
No. Generally speaking, you should rely on functions and language constructs to behave according to their documentation. Especially functions defined by the C language itself. Almost as much so functions defined by the platform's core specifications, such as POSIX on a POSIX-conforming system.
Also, generally speaking, you should approach compiler options with care and diligence. Some produce non-conforming behavior by design. Some come with caveats. And if I see an option that comes with such lengthy documentation as GCC's -flto
does, I usually take it as a sign that it is an expert feature that I shouldn't mess with unless I've invested the time and effort to make myself an expert with it.