首页 > 编程语言 > 详细

objective c - 单例模式(多线程)

时间:2014-09-10 22:34:01      阅读:341      评论:0      收藏:0      [点我收藏+]

//基于gcd的单例模式

static Type *_instances;

+ (id)allocWithZone:(NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
    _instances = [super allocWithZone:zone];
});
     return _instances;
}
//创建一个获取单例的方法
+ (Type *)sharedInstances
{
    if(_instances == nil){
       _instances = [[self alloc] init];
    }
    return _instances;
}


并且所有读取单例的地方都要用@synchronized “加锁”

objective c - 单例模式(多线程)

原文:http://blog.csdn.net/chenchaoflight/article/details/39186859

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!