From: Lailoken on
On May 27, 12:08 pm, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
> As the standard now defines multi-threading, is there anything said
> about the initialization of global variables? I.e. will a compiler be
> allowed to initialize global data in parallel?

I have always developed as if global variables were initialized
concurrently. Same with problems stemming from singleton design
patterns using function-local static variables:

http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B

and the static initialization order fiasco:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12

Even though I do not import external modules/dlls where execution or
thread-creation is unknown I would still use proper synchronization
personally. I trust nothing.

Marius.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 28 May, 18:11, Anthony Williams <anthony....(a)gmail.com> wrote:
> DeMarcus <use_my_alias_h...(a)hotmail.com> writes:
> > As the standard now defines multi-threading, is there anything said
> > about the initialization of global variables? I.e. will a compiler be
> > allowed to initialize global data in parallel?
>
> Yes. This is allowed by 3.6.2p2 in the FCD.
>
> The compiler still has to obey the ordering requirements. For example,
> within a single translation unit objects with static storage duration
> must be initialized in order of definition.

Would there be any practical point in doing this?

It seems to me that you still have to wait for everyting to be
initialized before main (unless the compiler adds a whole lot of extra
overhead or you are prepared to break things) so unless the compiler
somehow knew that certain ctors were expensive (a huge ask) it could
easily be more expensive to launch and synchronize the threads than to
just do it all on one thread as "usual".

I can't see why any compiler would chose to do this when anyone who
really wanted this behaviour could effectively program it explicitly
from the expensive ctor.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Peter C. Chapin on
Nick Hounsome wrote:

> It seems to me that you still have to wait for everyting to be
> initialized before main (unless the compiler adds a whole lot of extra
> overhead or you are prepared to break things) so unless the compiler
> somehow knew that certain ctors were expensive (a huge ask) it could
> easily be more expensive to launch and synchronize the threads than to
> just do it all on one thread as "usual".

The compiler might already be initializing some sort of thread pool to support
other features. In that case, the additional overhead of using multiple
threads during initialization might be slight.

Peter


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]