Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:42:06+00:00 2026-05-23T09:42:06+00:00

Consider this: class A { public: A(const char* s) { /*…*/ }; static const

  • 0

Consider this:

class A {
public:
    A(const char* s) { /*...*/ };
    static const A& get_default() { /* avoid static data memember init fiasco */
        static const A& a = A("default"); /* ..but not thread-safe */
        return a;
    }
};

How can I make get_default() thread safe? Or how can I find a way to get the A::a avoiding both the data member init fiasco and concurrency. I prefer not to use any locks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:42:07+00:00Added an answer on May 23, 2026 at 9:42 am

    The following will take advantage of the fact that initialization before main() is called will take place on a single thread, and that the pInstance pointer must be zero initialized before dynamic initialization of static objects takes place (3.6.2 “Initialization of non-local objects”).

    class A {
    public:
        A(const char* s) { /*...*/ };
    
        static const A& get_default() { 
            if (!pInstance) {
                static const A default_instance("default");
                pInstance = &default_instance;
            }
            return *pInstance;
        }
    
    private:
        static A const* pInstance;
    };
    
    A const* A::pInstance = &A::get_default();
    

    So, pInstance will be NULL the first time that get_default() is called – whether or not that call is due to the initialization of A::pInstance. That will cause get_default() to initialize it (pInstance will get ‘needlessly’ overwritten with the same value whenever it’s initializer runs – but that’ll be on the single init thread, so there are no threading issues). And just to be clear, the initializer for pInstance will force get_default() to be called on the init thread even if nothing else does.

    Subsequent calls to get_default() will not modify pInstance, so it’s threadsafe.

    Note that the simpler:

    class A {
    public:
        A(const char* s) { /*...*/ };
    
        static const A& get_default() { 
            static const A& a = A("default"); /* ..but not thread-safe */
            return a;
        }
    };
    
    namespace {
        A const& trigger = A::get_default();
    }
    

    Should also be threadsafe for the same reason, but it has a couple areas that make me a little less certain that it won’t have some potential drawbacks:

    • if the toolchain determines that trigger isn’t used elsewhere (and this applies even if we take it out of the anonymous namespace), I’d worry that it might get removed from the program image. I’m not sure about what promises the standard might make about preventing this optimization – particularly if class A lives in a library.
    • presumably the compiler simply checks a hidden static flag to determine if the initialization of the local static variable in get_default() has been run before. However, there’s really no promise about what mechanism is used (I haven’t looked into what C++0x might say about this). With the first example, the threadsafe check after the init time get_default() call is right there – guaranteed.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1;
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
Consider this class: public class TestMap extends HashMap<String, Float> { public static void main(String[]
Consider this: public class TestClass { private String a; private String b; public TestClass()
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I have an interesting problem. Consider this class hierachy: class Base { public: virtual
Consider this interesting set of types: class A { public virtual int MyProperty {
consider the following program: class Base { public: virtual void foo() const { cout
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider this example (typical in OOP books): I have an Animal class, where each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.