In C++11, the following is thread-safe:
void someFunc()
{
static MyObject object;
}
But what about
void someFunc()
{
static MyObject *ptr = new MyObject();
}
Is this then thread-safe or not?
As it was mentioned in the comments by @Nawaz, it is possibile, that the MyObject constructor isn't thread-safe, so let's divide the question into parts:
1) If the ctor is thread-safe (it doesn't access any shared state), is this static MyObject *ptr = new MyObject();
thread-safe? In other words, is static int *ptr = new int(0);
thread-safe?
2) If the ctor isn't thread-safe, but the object is created only by calling someFunc
from different threads, and the constructor is never used from anywhere else, will this be thread-safe then?