IOS single-piece notation singleton

Source: Internet
Author: User

+ (instancetype) sharedinstance{    static bookmanager *sharedinstance = nil;     Static dispatch_once_t Oncetoken;    Dispatch_once (&oncetoken, ^{        = [[Self alloc] init];    });     return sharedinstance;}

Reference: http://blog.csdn.net/sakulafly/article/details/34948689

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.

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.

IOS single-piece notation singleton

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.