1

Just curious. Subj. How C++ compiler knows not to reorder lock_guard in the beginning of the block? How C++ standard regulates it? Thanks.

{
    lock_guard<mutex> g(m);  // why this is not reordered to after "a = b"?
    a = b;
}
dimgel
  • 61
  • 1
  • 7
  • 1
    *"Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated"* and reordering these expressions would also break the "as if" rule, so no, the compiler shall not reorder these statements. – Cory Kramer Oct 24 '17 at 12:43
  • memory barriers. next – sehe Oct 24 '17 at 12:43
  • In short: the compiler is a C++ compiler because it implements the C++ rules. And those rules tell the compiler how your code should be compiled. – MSalters Oct 24 '17 at 12:48

0 Answers0