0

I'm new! I have read "C++ and The Perils of Double-Checked Locking"

Is it possible Compiler reorder code into critical_section? I guess in that article "Lock" is critical_section. but I have known reordering couldn't happen into critical_section. what's the true? Please help me

  • The compiler (generally) assumes you have no race conditions (since that would be undefined behaviour) and optimizes accordingly. Also, there's the "as if" rule, so if the compiler can prove that its changes have no observable impact (as defined by the standard) then it *will* morph your code regardless of critical sections - you *have* to know the standard to know what is allowed by the optimizer and what is not. It's not trivial... – Jesper Juhl Oct 21 '16 at 20:11
  • Thank you for your answer! So we use volatile keyword with lock(lock will use for mutual exclusion) to prevent reordering right? Some post say critical_section has memory barrier so I was confusedㅠㅠ – chfhrqnfRhc Oct 22 '16 at 06:04

0 Answers0