Since design patterns have emerged, disputes about singleton patterns have persisted. Most of the time we need a global thing, to ensure that there is only one copy, this time the single case is the best choice, but in the multi-threaded environment also need to do a good job of thread protection.
UIApplication and Nsfilemanager under iOS are good examples-we always need to use singleton mode. But to write the code is worth a bit of scrutiny:
The simplest example is as follows, assuming we have a testclass class that needs to implement a singleton:
+ (ID) sharedinstance { static testclass *sharedinstance = nil; if (!sharedinstance) { sharedinstance = [[Self alloc] init]; } return sharedinstance;}
of course, you can see at a glance that there is no thread-safe issue at all, let's add a thread lock.
+ (ID) sharedinstance { static testclass *sharedinstance = nil; @synchronized (self) { if (!sharedinstance) { sharedinstance = [[Self alloc] init]; } } return sharedinstance;}
This is a very common way of writing, if you continue, you need to overload retaincount,retain, and so on, this Apple official document has discussed, even has the example.
However, after the launch of GCD, there is a dispatch_once method that can be simpler to implement in a single instance, and the Dispatch_once function prototype is as follows:
void Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);
We can see that this function receives a dispatch_once_t parameter and a block parameter. For a given predicate, the function guarantees that the relevant block is bound to execute and executes only once, and most importantly-the method is fully thread-safe. It is important to note that for blocks that need to be executed only once, the incoming predicate must be identical, so predicate is often decorated with static or global.
+ (ID) sharedinstance { static testclass *sharedinstance = nil; static dispatch_once_t once; Dispatch_once (&once, ^{ sharedinstance = [[Self alloc] init]; }); return sharedinstance;}
The benefits of this writing are obvious: The code is simple and clear, and thread-safe. In addition, the dispatch_once is efficient and does not implement a heavy-weight synchronization mechanism, so the efficiency of the implementation is also high.