https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98108
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- I think it's QoI whether this works. "The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, ..." "The results of including <iostream> in a translation unit shall be as if <iostream> defined an instance of ios_base::Init with static storage duration." So there's no guarantee that the global iostreams will be usable during the static initialization phase, because there's no guarantee that a given constructor will be before the first ios_base::Init constructor. The problem is *not* that the counters use atomics rather than waiting. Even if the initialization is done as in the patch below, it still crashes: --- a/libstdc++-v3/src/c++98/ios_init.cc +++ b/libstdc++-v3/src/c++98/ios_init.cc @@ -77,7 +77,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ios_base::Init::Init() { - if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) + struct do_init + { + do_init() { // Standard streams default to synced with "C" operations. _S_synced_with_stdio = true; @@ -116,8 +118,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // streams are not re-initialized with uses of ios_base::Init // besides <iostream> static object, ie just using <ios> with // ios_base::Init objects. - __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); + _S_refcount = 1; } + }; +#ifndef __cpp_threadsafe_static_init +# warning "ios_base::Init::Init() initialization is not thread-safe" +#endif + static const do_init once __attribute__((unused)) = do_init(); + + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1); } ios_base::Init::~Init() This doesn't help, because the thread1 and thread2 objects in file1.cc are still constructed before the static ios_base::Init object in file2.cc and so they still try to use std::cout before the first _S_refcount increment has happened. Waiting for the iostream initialization to finish doesn't help if it hasn't started yet. And isn't relevant in your program, because only one ios_base::Init constructor ever runs in the whole program. Making the second one wait for the first doesn't help if there is only one. What does make your program work is: --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -71,7 +71,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION //@} // For construction of filebuffers for cout, cin, cerr, clog et. al. - static ios_base::Init __ioinit; + static ios_base::Init __ioinit __attribute__((__init_priority__(50))); _GLIBCXX_END_NAMESPACE_VERSION } // namespace And this works with or without changing the Schwarz counter.