1#import "EOCClassShareInstance.h" 2 3@implementation EOCClassShareInstance 4/** 5 * 一般创建单例方式 6 */ 7+(id)shareInstance{ 8 static EOCClassShareInstance *shared = nil; 9 @synchronized(self) { 10 if (!shared){ 11 shared = [[EOCClassShareInstance alloc]init]; 12 } 13 } 14 return shared; 15} 16/** 17 * GCD线程安全模式 18 */ 19+(id)sharedGCDInstance{ 20 static EOCClassShareInstance *sharedGCD = nil; 21 static dispatch_once_t onceToken;//每次调用都必须使用相同的标记,所以要申明为static 22 dispatch_once(&onceToken, ^{ 23 sharedGCD = [[self alloc]init]; 24 }); 25 return sharedGCD; 26} 27 28@end
OC高效率52之使用dispatch_once来执行只需运行一次的线程安全代码
Stella981
2021-10-11
1018 0 0
点赞
收藏
评论区
加载中...