CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] local static variables and thread safety

    Hello.

    I need some quick help here: Are local static variables thread-safe?

    I'm thinking about something like this:

    Code:
    MyClass& MyClass::GetInst()
    {
          static MyClass myObj;
          return myObj;
    }
    Is it ok?

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: local static variables and thread safety

    in c++03 no, it isn't
    in c++0x yes, it is ( threads entering concurrently in GetInst will wait for the initialization of myObj to complete )
    in VC++2010, AFAIK no, it isn't, being not implemented yet
    in latest gcc, I don't know; anybody ?

  3. #3
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: local static variables and thread safety

    So, the c++ standard says nothing about thread-safety of local static variables?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: local static variables and thread safety

    >> So, the c++ standard says nothing about thread-safety of local static variables?

    >> in c++03 no

    >> in c++11 yes
    They are thread-safe in the latest standard.

    gg

  5. #5
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: local static variables and thread safety

    And what about the static data members?
    I suppose, but I'm not 100% sure, that they're ok. Am I right?

    And, is there a workaround to making a class singletone besides using pointers?
    ie besides this:
    Code:
    class MyClass
    {
        MyClass* m_pInst;
       ....
    };
    
    MyClass* MyClass::GetInst()
    {
          if (!m_pInst) m_pInst = new MyClass;
          return m_pInst;
    }
    Last edited by Feoggou; October 11th, 2011 at 10:06 AM.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: local static variables and thread safety

    Static data members are initialized the same as a variable at global scope. This occurs before main() and before any threads are created.

    gg

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: local static variables and thread safety

    Quote Originally Posted by Feoggou View Post
    And, is there a workaround to making a class singletone besides using pointers?
    "workaround" usually implies a problem. What is the problem with a pointer?

    If what you want is for the instance to be in the static section, you can always do this:

    Code:
    class MyClass
    {
    private:
      static MyClass m_gInst;
    public:
      static MyClass& GetInst()
      {return MyClass::m_gInst;}
    };
    
    //Inside cpp
    MyClass MyClass::m_gInst;
    This requires greedy initialization, but is thread safe.

    *OR*


    [EDIT]ON SECOND THOUGHT: DON'T DO THIS. See Codeplug's post for why.[/EDIT]
    Another alternative to to declare a table to house the instance of your class, and merely construct it on demand. This requires an external static/extern though, given the use of the "sizeof" operator. It also needs a helper "instanciated" bool:

    Code:
    class MyClassHelper;
    class MyClass
    {
    private:
      static bool instanciated;
    public:
      static MyClass& GetInst();
    };
    
    class MyClassHelper
    {
      friend class MyClass;
      static char m_gInst[sizeof(MyClass)];
    };
    
    MyClass& MyClass::GetInst()
    {
      if(MyClass::instanciated == false)
      {
        new (MyClassHelper::m_gInst) MyClass;
        MyClass::instanciated = true;
      }
      return * reinterpret_cast<MyClass*>(MyClassHelper::m_gInst);
    }
    
    //in cpp
    bool MyClass::instanciated = false;
    char MyClassHelper::m_gInst[sizeof(MyClass)];
    Note that this is just as thread-unsafe as any other classic singleton though.
    Last edited by monarch_dodra; October 11th, 2011 at 01:39 PM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

    Re: local static variables and thread safety

    >> static char m_gInst[sizeof(MyClass)];
    This does not guarantee proper alignment. http://www.gotw.ca/gotw/028.htm

    gg

  9. #9
    Join Date
    Oct 2008
    Posts
    1,456

    Re: local static variables and thread safety

    Quote Originally Posted by monarch_dodra View Post
    [EDIT]ON SECOND THOUGHT: DON'T DO THIS. See Codeplug's post for why.[/EDIT]
    as a side note, you can use boost optional ( or in some situations boost variant ) to defer construction of stack variables without worrying of alignment issues:

    Code:
    boost::optional<MyClass> x;
    
    // ...
    
    if( !x ) x = boost::in_place( <<MyClass's ctor args>> );

  10. #10
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: local static variables and thread safety

    thanks for your help guys.
    Last edited by Feoggou; January 10th, 2012 at 03:38 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured