Undefined behaviour. You have two non-atomic accesses to a variable, one
of which is a write, with no defined order between them. This is
therefore a data race and undefined behaviour.
> Therefor, what's difference between memory_order_relaxed and no memory
> order in c++0x?

The use of atomics with memory_order_relaxed make it defined
behaviour. With atomics, the value of x is either 0 or 1, the same for
y. With plain int the program has undefined behaviour and may format
your hard disk.
> Does memory_order_relaxed prevent c++ compiler from code reordering or
> anything else ??

It ensures that the compiler uses the correct instructions for atomic
operations on the target CPU. It also may potentially inhibit some
optimizations.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!