-
October 11th, 2011, 08:37 AM
#1
[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?
-
October 11th, 2011, 09:05 AM
#2
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 ?
-
October 11th, 2011, 09:17 AM
#3
Re: local static variables and thread safety
So, the c++ standard says nothing about thread-safety of local static variables?
-
October 11th, 2011, 09:44 AM
#4
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
-
October 11th, 2011, 09:57 AM
#5
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.
-
October 11th, 2011, 10:03 AM
#6
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
-
October 11th, 2011, 10:42 AM
#7
Re: local static variables and thread safety
Originally Posted by Feoggou
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.
-
October 11th, 2011, 11:36 AM
#8
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
-
October 12th, 2011, 02:40 AM
#9
Re: local static variables and thread safety
Originally Posted by monarch_dodra
[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>> );
-
January 10th, 2012, 03:35 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|