原子性质和用途 [英] Atomic property and usage

查看:79
本文介绍了原子性质和用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于stackoverflow的答案,例如原子属性线程安全吗?何时使用@atomic?Objective-C中的原子属性与线程安全,但我对此有疑问:

I have read many stackoverflow answers for this like Is an atomic property thread safe?, When to use @atomic? or Atomic properties vs thread-safe in Objective-C but I have question for this:

请更正我,如果我错了,这就像我使用的是已通过Atomic属性声明的count变量,当前其值为5 这是由两个线程访问的,第一个线程将计数值增加2,第二个线程将计数值减少1,据我的理解,这就像第一个线程将其值现在增加5 + 2 = 7时一样;之后,只有第2个线程才能访问计数变量,并且只能将其值减1,即7-1 = 6?

Please correct me If I am wrong, This is like that I am using a count variable which I have declared with Atomic property and currently its value is 5 which is accessed by two thread , First thread that is increasing count value by 2 and 2nd thread decreasing the count value by 1 ,According to my understanding this go sequentially like when first thread increased its value which is now 5 + 2 = 7; after then only 2nd thread can access count variable and only decrease its value by 1 and that is 7 - 1 = 6?

推荐答案

基于原子的属性,其中两个线程正在访问同一对象以增加或减少一个值,您可以理解这一点,就像两个线程都访问了相同的/整个值为5一样但是第一个线程尝试更新此值,然后锁定该线程,没有其他线程可以同时更新此值,但是第二个线程也具有相同的值5,并且仅当第一个线程退出Count对象更新时才可以更新.

Atomic based property where two threads are accessing same object to increase or decrease a value , You can understand this like both have accessed the same/whole value which is 5 but First thread trying to update this value then this is locked no other thread can update this at the same time however 2nd thread is also having same value which is 5 and can update only when Count object updation get exited by first thread.

这篇关于原子性质和用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆