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

  • SEARCH
  • Home
  • 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 this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)
Consider this code: public static class PredefinedStrings { public const string Golf = Golfing;
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char
Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1;
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider this class Foo { public: Foo(){} ~Foo(){} void NonConstBar() {} void ConstBar() const
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
Consider the following : class A { public: int xx; A(const A& other) {

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.