This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/16353] New: static function variable initialization not thread safe


I believe that the following piece of code should be thread-safe:

#include <memory>

struct A {};

template <typename T>
T * Singleton()
{
    // the following constructor should be called
    // exacrlt once but seems unsafe in MT code
    static std::auto_ptr<T> l_singleton_ptr( new T );
    return &*l_singleton_ptr;
}

Even thought the standard does not specify threading, it does guarentee that
l_singleton_ptr is created exactly once.  With GCC 3.4.0, this does not seem to
hold for multi-threaded applications.  GCC does support multi-threaded
basic_string and new (to name a few), but guarenteeing the constructor being
called only once for static function variables seems to be a very important and
vary hard (to almost impossible) to work-around.

-- 
           Summary: static function variable initialization not thread safe
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gianni at mariani dot ws
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16353


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]