设为首页 加入收藏

TOP

objective c - 单例模式(多线程)
2015-01-22 20:56:28 来源: 作者: 【 】 浏览:11
Tags:objective 单例 模式 线程

//基于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 “加锁”
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇c语言之assert的使用 下一篇Objective-C――类、方法、属性、..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: