Objective-C中的原子属性与线程安全 [英] Atomic properties vs thread-safe in Objective-C

查看:97
本文介绍了Objective-C中的原子属性与线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我阅读的大多数讨论中,它表明将属性设为原子并不保证它是线程安全的,它只是保证所返回的值不会由于一个对象写入而成为垃圾.它和另一个试图同时阅读它.

In most of the discussions I've read, it indicates that making a property atomic does not guarantee it to be thread-safe, it just guarantees that the value returned won't be garbage as a result of one object writing to it and another trying to read it at the same time.

我知道这不是线程安全的,因为第三个对象可能正在写它,而访问它的对象不会得到垃圾回收,但是并不确定当多个对象正在写入时它将返回哪个值同时,它可能会获得它们的任何值.

I understand this isn't thread-safe as a third object could be writing it and while the object accessing it wouldn't get garbage back, it's not entirely certain which value it will get back as multiple objects are writing to it at the same time, and it may get any of their values.

因此,当我们说它不会返回垃圾时,垃圾的含义是,如果一个对象不是原子的,而另一个对象正在写入该对象时试图访问该对象,则该结果可能会返回中值,写,并且只获得写带来的更改的部分,不完整版本?从这个意义上说,这是垃圾"的意思吗?原子的性质有助于防止这种情况吗?

So when we say it won't return garbage, would garbage be in the sense that if an object was non-atomic and an object tried to access it while another was writing to it, it might get the result back mid-write, and only get a partial, incomplete version of the change brought about by the write? Is this what "garbage" means in this sense, and what atomic properties help to prevent?

推荐答案

Objective C中的atomic属性保证您永远不会看到部分写入. 当@property具有属性atomic时,不可能仅部分写入该值.设置者是这样的:

An atomic property in Objective C guarantees that you will never see partial writes. When a @property has the attribute atomic it is impossible to only partially write the value. The setter is like that:

- (void)setProp:(NSString *)newValue {
    [_prop lock];
    _prop = newValue;
    [_prop unlock];
}

因此,如果两个线程想同时写入值@"test"和@"otherTest",则 在任何给定时间,该属性只能是该属性的初始值或@"test"或@"otherTest". nonatomic更快,但该值是垃圾值,并且没有@"test"/@"otherTest"(thx @Gavin)或任何其他垃圾值的部分String.

So if two thread want to write the value @"test" and @"otherTest" at the same time, then at any given time the property can only be the initial value of the property or @"test" or @"otherTest". nonatomic is faster but the value is a garbage value and no partial String of @"test"/@"otherTest" (thx @Gavin) or any other garbage value.

但是atomic仅具有简单使用的线程安全性. 它不被保证. Appledoc 说:

But atomic is only thread-safe with simple use. It is not garantueed. Appledoc says the following:

考虑一个XYZPerson对象,其中一个人的第一个和最后一个 使用一个线程的原子访问器更改名称.如果另一个 线程同时访问两个名称,即原子获取方法 将返回完整的字符串(不会崩溃),但是没有 确保这些值将是相对于每个值的正确名称 其他.如果在更改之前访问了名字,但在更改后访问了名字 更改后访问名称,结果会不一致, 一对不匹配的名称.

Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.

我完全没有使用原子的问题.我以这种方式设计了代码,原子属性没有问题.

I never had a problem using atomic at all. I designed the code that way, that there is not problem with atomic properties.

这篇关于Objective-C中的原子属性与线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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