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

查看:19
本文介绍了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)或任何其他垃圾值.

So if two threads 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 对象,其中一个人的第一个和最后一个使用一个线程中的原子访问器更改名称.如果另一个线程同时访问两个名称,原子 getter 方法将返回完整的字符串(不会崩溃),但没有保证这些值是相对于每个值的正确名称其他.如果在更改之前访问了名字,但最后一个更改后访问名称,您最终会得到不一致的,不匹配的名称对.

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.

我在使用 atomic 时从来没有遇到过问题.我是这样设计代码的,原子属性没有问题.

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天全站免登陆