Oleg Zabluda's blog
Friday, September 21, 2012
 
Many C/C++ developers are losing sleep over a simple question: modulo data races and other undefined behavior, is it...
Many C/C++ developers are losing sleep over a simple question: modulo data races and other undefined behavior, is it possible for both asserts not to trigger:

int x;
x=0; // maybe in the other thread, assume sync
x=1; // maybe in the other thread, assume sync
assert(x==1);
assert(x==0);   

In single-threaded programs, it was never possible (program order). But in multithreaded, anything was possible. Before 2011 that is. 

You may be glad to know that both C11/C++11 guarantee that, from now on, what has been seen, can not be unseen or, more succinctly, you can't unsee the horror. If you saw x==1, you can't unsee it and see the older value x==0.

http://knowyourmeme.com/memes/what-has-been-seen-cannot-be-unseen

Until such time when such simple explanations are routinely put into the standards, you have to get by with just as clear standartese:

============================
SO/IEC 14882:2011 aka C++11:

1.10 Multi-threaded executions and data races

13. A visible side effect A on a scalar object or bit-field M with respect to a value computation B of M satisfies the conditions:
— A happens before B and
— there is no other side effect X to M such that A happens before X and X happens before B.

The value of a non-atomic scalar object or bit-field M, as determined by evaluation B, shall be the value stored by the visible side effect A.

=====================
ISO/IEC 9899:2011 aka C11 has a similar language:

5.1.2.4 Multi-threaded executions and data races

19. A visible side effect A on an object M with respect to a value computation B of M satisfies the conditions:
— A happens before B, and
— there is no other side effect X to M such that A happens before X and X happens before B.

The value of a non-atomic scalar object M, as determined by evaluation B, shall be the value stored by the visible side effect A.

=====================

P.S. Inspired by Matt Austern's C++11 series of posts, but his needs more cats.



Labels:


| |

Home

Powered by Blogger