From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3168 invoked by alias); 4 Nov 2003 09:17:53 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 3160 invoked by uid 48); 4 Nov 2003 09:17:52 -0000 Date: Tue, 04 Nov 2003 09:17:00 -0000 Message-ID: <20031104091752.3159.qmail@sources.redhat.com> From: "peturr02 at ru dot is" To: gcc-bugs@gcc.gnu.org In-Reply-To: <20031031094301.12855.peturr02@ru.is> References: <20031031094301.12855.peturr02@ru.is> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug libstdc++/12855] Thread safety problems in ios_base::Init X-Bugzilla-Reason: CC X-SW-Source: 2003-11/txt/msg00213.txt.bz2 List-Id: PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12855 ------- Additional Comments From peturr02 at ru dot is 2003-11-04 09:17 ------- > std::ios_base::Init should never be used outside global objects. What about logging? Consider: Foo.h: ============================ class Foo { public: Foo(); ~Foo(): }; ============================ foo.cc: ============================ #include Foo::Foo() { std::clog << "Foo constructed\n"; } Foo::~Foo() { std::clog << "Foo destroyed\n"; } ============================ If we have another source file: a.cc: ============================ #include "foo.h" static Foo foo; ============================ This is unsafe, because foo may be constructed before the ios_base::Init object in foo.cc, and Foo::Foo() may access clog before it is constructed. This can be fixed by modifying foo.cc: ============================ #include Foo::Foo() { std::ios_base::Init init; std::clog << "Foo constructed\n"; } Foo::~Foo() { std::ios_base::Init init; std::clog << "Foo destroyed\n"; } ============================ The standard guarantees that clog will be constructed before the constructor of init finishes, so the problem has been fixed. However, unless ios_base::Init is threadsafe, this means that Foo can no longer be used safely in threaded programs.