Re: Is this reordering valid?
pongba@gmail.com wrote:
C* pc = new C();
It involves three steps:
1. allocation of raw memory
2. construction of C
3. assignment
The question is, is it really allowed by the standard that 2 and 3
could be reordered?
Before you start to judge, let me show you this little chunk of code:
struct C{
C()
{
throw std::runtime_exception("...");
}
};
Good example, but IMHO it does not address the actual problem.
There is a difference between executing a constructor (it's when the
exception might get thrown) and having all the assignments from that
constructor become visible to the reader. Throwing an exception just
breaks the control flow (at point 2.) and that's why there is no way for
you to see assignment (point 3. above) after that - it just never
happens. But if the constructor performs some assignments on its own
(and very likely it does, otherwise it would be useless), completes and
then the pointer value is assigned to pc, then from the point of view of
the CPU this is all just a single sequence of assignments. Without any
synchronization, they will be visible in the "correct" order from the
same thread (sequence points and so on), but there is no provision that
another thread will see them in the same order. For this, you need to
work harder (mutex, membar, etc.).
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]