From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5CE6D396E419; Wed, 2 Dec 2020 21:32:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5CE6D396E419 From: "i.hamsa at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/98108] New: Broken Schwarz counter for iostreams initialization Date: Wed, 02 Dec 2020 21:32:36 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new 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: i.hamsa at gmail dot com 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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, 02 Dec 2020 21:32:36 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98108 Bug ID: 98108 Summary: Broken Schwarz counter for iostreams initialization Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: i.hamsa at gmail dot com Target Milestone: --- Iostreams initialization doesn't work in multithreaded environments. To reproduce one needs two source files: // file1.cc #include =20=20 void threadfunc();=20=20 struct StaticThread { std::thread t;=20=20 StaticThread() : t(threadfunc) {} ~StaticThread() { t.join(); } };=20=20 static StaticThread thread1, thread2; //file2.cc #include =20=20 void threadfunc() { std::cout << "Printing\n"; }=20=20 int main() {} //compile g++ -pthread file1.cc file2.cc It is important that is NOT included in file1.cc. The resulting executable always crashes on my machine (Gentoo Linux). Add to file1.cc, the crash disappears. Change the order of source fi= les in the command line, the crash disappears. --- The culprit is ios_base::Init::_S_refcount. Basically it is an atomic nifty counter. The problem is, the nifty counter idiom doesn't work with multithreading. Making it atomic achieves absolutely nothing. If we have tw= o or more threads that can enter Init::Init() at the same time, one should proce= ed and the rest must WAIT. It is the same thing as initialization of a static object in a block scope, and should be interlocked in exactly the same way.=