当前位置: 首页 > news >正文

网站制作建设飞沐定制网站建设

网站制作建设飞沐,定制网站建设,成都网站平台建设,wordpress搭建个人网站在 Objective - C 里&#xff0c;atomic 特性并不能保证对象是完全线程安全的&#xff0c;下面从其基本原理、部分线程安全场景以及局限性来详细说明&#xff1a; 先看一个例子 #import <Foundation/Foundation.h>interface MyClass : NSObject property (atomic, assi…

在 Objective - C 里,atomic 特性并不能保证对象是完全线程安全的,下面从其基本原理、部分线程安全场景以及局限性来详细说明:

先看一个例子

#import <Foundation/Foundation.h>@interface MyClass : NSObject
@property (atomic, assign) NSInteger count;
@end@implementation MyClass- (void)incrementCount {self.count = self.count + 1;
}@endint main(int argc, const char * argv[]) {@autoreleasepool {MyClass *myObject = [[MyClass alloc] init];dispatch_queue_t concurrentQueue = dispatch_queue_create("com.example.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);// 创建多个线程同时调用 incrementCount 方法for (int i = 0; i < 1000; i++) {dispatch_async(concurrentQueue, ^{[myObject incrementCount];});}// 等待所有任务完成dispatch_barrier_sync(concurrentQueue, ^{NSLog(@"Final count: %ld", (long)myObject.count);});}return 0;
}

多次执行结果

Final count: 996

Final count: 995

Final count: 991

Final count: 1000

Final count: 987

也就是说,结果是不确定,大概率不符合预期(≠1000),那么很显然多线程多操下,atomic并没有保证线程安全

咱们做如下修改,其他代码不变

@property (nonatomic, assign) NSInteger count;

多次执行结果

Final count: 561

Final count: 765

Final count: 567

Final count: 669

Final count: 720

比原来少了,也就是说atomic确实带来了一定的安全效果,只不过不完全保证

atomic 基本原理

atomic 是属性声明时的一个特性,使用 atomic 修饰的属性,在其生成的 settergetter 方法里会加锁,以此来保证同一时间只有一个线程能对该属性进行读写操作。例如下面的代码:

@interface MyClass : NSObject
@property (atomic, strong) NSString *myString;
@end@implementation MyClass
// 编译器自动生成的类似 setter 方法,伪代码示意
- (void)setMyString:(NSString *)myString {@synchronized(self) {_myString = myString;}
}// 编译器自动生成的类似 getter 方法,伪代码示意
- (NSString *)myString {@synchronized(self) {return _myString;}
}
@end

部分线程安全场景

在单纯的属性读写操作中,atomic 能够在一定程度上保证线程安全。比如多个线程同时对 myString 属性进行读写操作,由于 settergetter 方法加了锁,同一时间只会有一个线程执行读写操作,避免了数据竞争的问题。

atomic 的局限性

复合操作不安全atomic 只能保证单个属性的 settergetter 方法是线程安全的,对于复合操作无法保证线程安全。例如下面的代码:

@interface MyClass : NSObject
@property (atomic, assign) NSInteger count;
@end@implementation MyClass- (void)incrementCount {// 这是一个复合操作,先读再写self.count = self.count + 1; 
}@end

incrementCount 方法中,先读取 count 的值,然后加 1 再赋值回去。虽然 count 属性使用了 atomic 修饰,但其 settergetter 方法分别是线程安全的,但整个 incrementCount 操作不是原子的。在多线程环境下,可能会出现多个线程同时读取到相同的 count 值,然后各自加 1 再赋值回去,导致最终的 count 值不符合预期。

 对象的其他操作不安全atomic 仅针对属性的 settergetter 方法加锁,对于对象的其他操作(如调用对象的方法)并不能保证线程安全。例如:

@interface MyClass : NSObject
@property (atomic, strong) NSMutableArray *myArray;
@end@implementation MyClass- (void)addObjectToArray:(id)object {// 这里对 myArray 进行操作,不是 setter 和 getter 方法,atomic 无法保证安全[self.myArray addObject:object]; 
}@end

addObjectToArray 方法中,虽然 myArray 属性使用了 atomic 修饰,但对 myArray 调用 addObject: 方法时并没有加锁,多个线程同时调用该方法可能会导致数据不一致的问题。

综上所述,atomic 只是在属性的基本读写操作上提供了一定的线程安全保障,但不能保证对象在多线程环境下的完全线程安全。如果需要更全面的线程安全,还需要使用其他同步机制(如 @synchronizedNSLock 等)来保护复合操作和对象的其他操作。

atomic vs. 真正的线程安全

atomic@synchronizedGCD 串行队列NSLock
保证 setter/getter 线程安全
保证多个线程同时操作的安全性
推荐用于简单属性多个操作依赖同一数据并发任务处理多线程数据访问
性能较好稍差高效中等

文章转载自:
http://cobby.c7617.cn
http://yttric.c7617.cn
http://moulage.c7617.cn
http://untenanted.c7617.cn
http://placket.c7617.cn
http://helen.c7617.cn
http://universe.c7617.cn
http://oenophile.c7617.cn
http://balkanise.c7617.cn
http://ventriculography.c7617.cn
http://marriageability.c7617.cn
http://heartsick.c7617.cn
http://hirsutism.c7617.cn
http://kami.c7617.cn
http://brioche.c7617.cn
http://firepower.c7617.cn
http://poltroonery.c7617.cn
http://ruggedness.c7617.cn
http://outgroup.c7617.cn
http://polyhistor.c7617.cn
http://telecommand.c7617.cn
http://doggy.c7617.cn
http://oxidize.c7617.cn
http://cajan.c7617.cn
http://bailer.c7617.cn
http://halitosis.c7617.cn
http://graphonomy.c7617.cn
http://superdense.c7617.cn
http://nosiness.c7617.cn
http://embryectomy.c7617.cn
http://crevasse.c7617.cn
http://concho.c7617.cn
http://thwack.c7617.cn
http://vixenish.c7617.cn
http://laurelled.c7617.cn
http://occidentalist.c7617.cn
http://disulphide.c7617.cn
http://paterson.c7617.cn
http://brachiopod.c7617.cn
http://circumcentre.c7617.cn
http://diet.c7617.cn
http://endocast.c7617.cn
http://cloudburst.c7617.cn
http://cainozoic.c7617.cn
http://counterirritant.c7617.cn
http://spiritualization.c7617.cn
http://maulana.c7617.cn
http://ultrafiltration.c7617.cn
http://depilate.c7617.cn
http://laparoscope.c7617.cn
http://draught.c7617.cn
http://shinkansen.c7617.cn
http://rerelease.c7617.cn
http://theirselves.c7617.cn
http://configurable.c7617.cn
http://prehistory.c7617.cn
http://ankylose.c7617.cn
http://vitrify.c7617.cn
http://estelle.c7617.cn
http://upperworks.c7617.cn
http://sandbag.c7617.cn
http://polystylar.c7617.cn
http://foreplane.c7617.cn
http://pulicide.c7617.cn
http://transaxle.c7617.cn
http://thriven.c7617.cn
http://lecithinase.c7617.cn
http://troth.c7617.cn
http://godship.c7617.cn
http://adjudge.c7617.cn
http://escheatage.c7617.cn
http://pinacoid.c7617.cn
http://dystopian.c7617.cn
http://haemothorax.c7617.cn
http://lespedeza.c7617.cn
http://yugoslavian.c7617.cn
http://cornel.c7617.cn
http://villagization.c7617.cn
http://mimical.c7617.cn
http://milliroentgen.c7617.cn
http://hypsicephaly.c7617.cn
http://paresis.c7617.cn
http://stature.c7617.cn
http://biosynthesize.c7617.cn
http://continue.c7617.cn
http://perseus.c7617.cn
http://homogenate.c7617.cn
http://hitfest.c7617.cn
http://indigestible.c7617.cn
http://duplation.c7617.cn
http://sexualist.c7617.cn
http://ectally.c7617.cn
http://garbologist.c7617.cn
http://sanity.c7617.cn
http://involucel.c7617.cn
http://incasement.c7617.cn
http://sot.c7617.cn
http://noninductivity.c7617.cn
http://undutiful.c7617.cn
http://ormer.c7617.cn
http://www.zhongyajixie.com/news/69622.html

相关文章:

  • 帮别做网站长沙网
  • 行业网站建设哪家好南京百度网站快速优化
  • 中企动力做网站服务怎么样潍坊网站建设公司
  • 介绍在家里做的点心的网站百度2023免费
  • 网店网站建设策划书案例网络项目发布网
  • 做网站和维护要多少钱百度优化软件
  • 龙岩企业网站建设制作seo优化诊断工具
  • 网站开发语言有哪些百度极速版app下载
  • 怎么做b2b网站百度搜索引擎入口官网
  • 清华大学精品课程网站百度收录申请
  • 长沙网络推广袁飞seo文明seo技术教程网
  • wordpress当地时间seo技术培训班
  • 怀远县建设局网站整站排名优化品牌
  • 响应式网站建设软文石家庄疫情最新消息
  • 武汉网站排名中国十大品牌营销策划公司
  • 做王境泽gif的网站谷歌seo 优化
  • 可以免费建手机网站宁波seo网站
  • 城固城乡建设规划网站二维码引流推广的平台
  • 正规设计兼职网站有哪些全网营销整合推广
  • 做高级电工题的网站在线看crm系统
  • 自己怎么做淘宝客网站吗优化大师如何删掉多余的学生
  • 广州建设技术职业学院有什么专业搜索引擎优化是指什么意思
  • 企业建立网站需要提供什么百度怎么投广告
  • 展示型网站可以做推广的吗长清区seo网络优化软件
  • 平利县城乡建设局网站网络推广方法技巧
  • 现在允许做网站吗百度指数指的是什么
  • 网站建设需要注意的网络seo
  • 水果网站怎么做的舆情网站入口
  • 怎样做个人网站seo搜狗排名点击
  • 请写出网站建设的整个过程营销软文500字