Answer yes and maybe no
The principles of the memory model:
C ++ 11 atomics uses the default order in memory order std::memory_order_seq_cst, which means that operations are sequentially sequential .
The semantics of this is that the ordering of all operations is performed as if all these operations were performed sequentially:
32.3 ++ , : " S memory_order_seq_cst, ", , memory_order_seq_cst
, , S, , memory_order_seq_cst. "
1.10/5 , : " (...), . , ".
- !
, .
, :
(thread 1) A.foo = 10;
(thread 1) A.foo = 4;
(thread 1) ptr.store(&A);
(thread 2) int i = *ptr;
i 4. ptr , thread (2) &A . , , ptr, ( " " ).
, :
(thread 1) A.foo = 4;
(thread 1) ptr.store(&A);
(thread 1) A.foo = 8;
(thread 2) int i = *ptr;
undefined. 4 - , , , ptr, . , . 8.
*ptr = 8; A.foo=8;, : i 8.
, :
void f1() {
secret = 50;
ptr = &secret;
secret = 777;
this_thread::yield();
}
void f2() {
this_thread::sleep_for(chrono::seconds(2));
int i = *ptr;
cout << "Value is " << i << endl;
}
, - , . , ptr . .
, .
:
std:atomic<Object*> ptr;
A.foo = 4;
ptr.store(*A);
Object *x;
x=ptr;
terrible_function(ptr);