From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 59D913842422; Wed, 9 Dec 2020 15:52:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59D913842422 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/98108] Broken Schwarz counter for iostreams initialization Date: Wed, 09 Dec 2020 15:52:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Dec 2020 15:52:08 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98108 --- Comment #3 from Jonathan Wakely --- I think it's QoI whether this works. "The objects are constructed and the associations are established at some t= ime prior to or during the first time an object of class ios_base::Init is constructed, ..." "The results of including in a translation unit shall be as if defined an instance of ios_base::Init with static storage durati= on." 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) =3D=3D 0) + struct do_init + { + do_init() { // Standard streams default to synced with "C" operations. _S_synced_with_stdio =3D true; @@ -116,8 +118,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // streams are not re-initialized with uses of ios_base::Init // besides static object, ie just using with // ios_base::Init objects. - __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); + _S_refcount =3D 1; } + }; +#ifndef __cpp_threadsafe_static_init +# warning "ios_base::Init::Init() initialization is not thread-safe" +#endif + static const do_init once __attribute__((unused)) =3D 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.=