note: i'm using objective-c++ where non-compile-time constant is allowed (https://stackoverflow.com/a/12304815/3101492)
+ (Foo)sharedFoo
{
static Foo *foo = [Foo new];
return foo;
}
static initializer is expected to be run only once, but is it thread safe such that if multiple threads call +(Foo)sharedFoo at same time, is it guaranteed that [Foo new] will be run only once?
I'm asking because if so, then why is it recommended that singleton patterns in obj-C use dispatch_once like the following?
+ (Foo)sharedFoo {
static Foo *foo = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
foo = [Foo new];
});
return foo;
}
i'm essentially asking why can't the first line be just
static Foo *foo = [Foo new];
and just skip dispatch_once altogether if we know static local var initialization only runs once..
edit: ah ok, i found an answer. 1. first, i realized i was using objective-c++ which allows above code to compile (and runs it during runtime instead) 2. second, compiler turns that code into the "naive" version of singleton initializer w/o dispatch_once such that it is indeed not thread-safe.