How does the C ++ compiler guarantee the security of a stream of an element constant variable?

C ++ 11x standard modified keyword semantics of "const". Now that means real thread safety. As far as I understand, the const member variable is equal to the final java field.

I want to test this on icore 7 g ++ 4.7.1 CPU.

I compiled the following code with -std = C ++ 0x -pthread -DCONST and without it. Both executables are identical. There are no assembly instructions in assembler versions. I expected to see the * fence at the end of the constructor.

class Big {
public:
#ifdef CONST
  const
#endif
  long a;
  Big(long a) : a(a) {
  }
  void check()
 #ifdef CONST
  const 
 #endif
  {
    assert(a == 123L);
  }
};

int main() {
  Big b(123L);
  thread t([b] () { b.check(); });
  return 0;
}
+4
source share
2 answers

, , , .

, - , . , "", "". .

  • , , .

  • A const , ( ).

  • , , > ( , , ).

# 2 # 3 , const , № 1.

... mutable , mutable, , , const, mutable, - .

, ... . const, mutable - , const , .

++ 11, , , , std:: , , .

Herb Sutter - [blank] [blank], , ++ 11 , const == -, .

+1

. , , const: , , . , , .

0

Source: https://habr.com/ru/post/1534462/


All Articles