IOS Singleton Mode

Source: Internet
Author: User

Recently, in iOS development, the singleton mode is required, so I wrote one by myself, which looks like this:

<1>

+ (id)sharedInstance{    static id sharedInstance = nil;        if (!sharedInstance) {        sharedInstance = [[NSObject alloc] init];    }        return sharedInstance;}

Later I found that many books use the bool variable as the bid. It looks like this:

<2>

+ (id)sharedInstance{    static id sharedInstance = nil;    static BOOL token = NO;        if (!token) {        token = YES;                sharedInstance = [[NSObject alloc] init];    }        return sharedInstance;}

However, according to Apple's official Singleton code, it looks like this:

<3>

+ (id)sharedInstance{    static id sharedInstance;    static dispatch_once_t onceToken;        dispatch_once(&onceToken, ^{        sharedInstance = [[NSObject alloc] init];    });        return sharedInstance;}

So what are the differences between them?

Originally, they differ in the performance of multi-thread concurrency.

<1> A pointer variable is used as a flag, which is not desirable in multi-thread concurrency because sharedinstance = [[nsobject alloc] init]; the execution of this line of code takes time. Two threads may enter this line of code at the same time, which may cause memory leakage.

<2> the flag bit used is a bool variable. When multiple threads are concurrent, the first thread is judged as no, alloc is started, and the value assignment is performed, but when two threads enter, the value is yes, and the value assignment operation of one thread is not completed yet. At this time, two threads will get a nil. Although it will not cause memory leakage, there will be a considerable number of threads that cannot get objects.

<3> the dispatch_once function is used. This function is derived from Grand Central Dispatch (GCD), which is referenced by Apple from Mac OS 10.6/IOS 4.0.

This function receives a dispatch_once_t used to check whether the code block has been scheduled (it is a long integer and is actually used as a bool ). It also receives a code block that is only scheduled once in the life cycle of the application. This not only means that the code is only run once, but also thread-safe. You do not need to use @ synchronized to prevent non-synchronization when multiple threads or queues are used.

Apple's GCD documentation confirms this:

If called by multiple threads, the function is synchronized until the code block is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.