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++/13487] Construction of local static objects is not thread-safe


------- Additional Comments From gianni at mariani dot ws  2004-07-04 00:36 -------
This is just to post code that actually compiles (pulling in the example from
bug 13487).

Also, I'd like to note that the standard requires that the destructors of static
variables be invoked in the reverse order of construction.  This means that the
mechanism that places static destuctor calls in the (I imagine) linked list
needs to also be thread safe.  Hence my earlier comment that it's almost
impossible for an external mechanism to accomplish the standard's requirements
in multi-threaded code.


#include <memory>
#include <iostream>

struct A {};

template <typename T>
T * Singleton()
{
    static std::auto_ptr<T> l_singleton_ptr( new T );
    return &*l_singleton_ptr;
}


int z( int a )
{
    if ( a & 1 )
    {
        static int v( a );
        return v;
    } else {
        static int v( a );
        return v;
    }
}


using namespace std;
int main()
{
    A * ptr = Singleton<A>();
    
    cout << z( 1 ) << " " << z( 2 ) << " ";
    cout << z( 3 ) << " " << z( 4 );
}



-- 


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


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