From: Steven Watanabe (steven_at_[hidden])
Date: 2007-01-06 18:02:10


AMDG

Andrey Semashev wrote:
> This is just wrong. It is perfectly legal
> > for the compiler to reorder it to be
> > if (!m_fInitialized)
> > {
> > m_fInitialized = true;
> > // Fill m_StatesInfo array for all states
> > const root_type* pRoot = pStates;
> > pStates->__init_states_info(reinterpret_cast< const char*
> >>(pRoot), m_StatesInfo);
>
>> > }
>> > then
>> > Thread A
>> > if (!m_fInitialized)
>> > {
>> > m_fInitialized = true;
>> > Thread B
>> > if (!m_fInitialized)
>> > {
>> > }
>> > //use m_StatesInfo.
>> > //die.
>>
> I'd rather say, your scenario is possible here but not necessarily it
> will happen. A compiler _may_ be smart enough to reorder expressions
> if it may prove that it won't break the code behavior (which is true
> here) and it will gain something (performance, for example) from it.
> And even if it does so consider that Thread A in your example may be
> interleaved by Thread B right between it checks "m_fInitialized" and
> stores "true" in it. So it is actually possible that more than one
> thread will execute the "true" branch of the "if" statement
> concurrently. But we're on the safe side here because only simple
> operations on POD types are involved in it.
I see that it is ok for m_StatesInfo to be initialized more than once.
My point was
that it can be used before it is initialized unless you make everything
involved volatile.

In Christ,
Steven Watanabe