Apple 对多线程引用和值类型的描述 [英] Apple's description of reference and value types with multiple threads

查看:17
本文介绍了Apple 对多线程引用和值类型的描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Apple 的文档.我以为我知道何时选择值类型以及何时选择引用类型,但我又回到了 Swif101.文档说:

I am reading from Apple's documentation. I thought I knew when to choose a value type and when to choose a reference type, but I am back to Swif101. The documentation says:

  • 值类型:数据将在多个线程的代码中使用.
  • 引用类型:您想要创建共享的、可变的状态
  • Value Types: The data will be used in code across multiple threads.
  • Reference Types: You want to create shared, mutable state

引用类型不是也可以跨多个线程共享吗?这两行有什么区别?

Aren't reference types also shared across multiple threads? What's the difference in these two lines?

推荐答案

正如其他人所指出的,引用类型总是传递一个指向对象的指针,这是您想要共享、可变状态"(如该文档)的理想选择你提到的说).但是,很明显,如果您跨多个线程改变/访问引用类型,请确保同步您对它的访问(通过专用串行队列、读写器模式、锁等).

As others have pointed out, reference types always pass a pointer to the object, which is ideal where you want a "shared, mutable state" (as that document you referenced said). Clearly, though, if you're mutating/accessing a reference type across multiple threads, make sure to synchronize your access to it (via a dedicated serial queue, the reader-writer pattern, locks, etc.).

不过,值类型稍微复杂一些.是的,正如其他人指出的那样,如果您将值类型作为参数传递给一个方法,然后该方法在另一个线程上执行某些操作,那么您实际上是在使用该值类型的副本(Josh 关于复制的说明-写,尽管如此).这确保了传递给方法的对象的完整性.没关系(并且已被此处的其他答案充分涵盖).

Value types are a little more complicated, though. Yes, as the others have pointed out, if you pass a value type as a parameter to a method that then does something on another thread, you're essentially working with a copy of that value type (Josh's note regarding the copy-on-write, notwithstanding). This ensures the integrity of that object passed to the method. That's fine (and has been sufficiently covered by the other answers here).

但是当你处理闭包时它会变得更加复杂.例如,考虑以下情况:

But it gets more complicated when you are dealing with closures. Consider, for example, the following:

struct Person {
    var firstName: String
    var lastName: String
}

var person = Person(firstName: "Rob", lastName: "Ryan")

DispatchQueue.global().async {
    Thread.sleep(forTimeInterval: 1)
    print("1: (person)")
}

person.firstName = "Rachel"
Thread.sleep(forTimeInterval: 2)
person.lastName = "Moore"
print("2: (person)")

显然,您通常不会sleep,但我这样做是为了说明这一点:也就是说,即使我们正在处理一个值类型和多个线程,您在闭包中引用的 person 与您在主线程(或正在运行的任何线程)上处理的 相同 实例,而不是它的副本.如果您正在处理可变对象,则这不是线程安全的.

Obviously, you wouldn't generally sleep, but I'm doing this to illustrate the point: Namely, even though we're dealing with a value type and multiple threads, the person you reference in the closure is the same instance as you're dealing with on the main thread (or whatever thread this was running on), not a copy of it. If you're dealing with a mutable object, that's not thread-safe.

我设计了这个例子来说明这一点,上面闭包中的 print 语句将报告Rachel Ryan",有效地显示了 Person 的状态状态不一致的值类型.

I've contrived this example to illustrate this point, where the print statement inside the closure above will report "Rachel Ryan", effectively showing the state of the Person value type in an inconsistent state.

使用值类型的闭包,如果你想享受值语义,你必须改变 async 调用以使用一个单独的变量:

With closures using value types, if you want to enjoy value semantics, you have to change that async call to use a separate variable:

let separatePerson = person
queue.async {
    Thread.sleep(forTimeInterval: 1)
    print("1: (separatePerson)")
}

或者,更简单的是,使用捕获列表",它指示闭包应该捕获哪些值类型变量:

Or, even easier, use a "capture list", which indicates what value type variables should be captured by the closure:

queue.async { [person] in
    Thread.sleep(forTimeInterval: 1)
    print("1: (person)")
}

使用这些示例中的任何一个,您现在都可以享受值语义,复制对象,并且 print 语句将正确报告Rob Ryan",即使原始 person 对象正在另一个线程上发生变异.

With either of these examples, you're now enjoying value semantics, copying the object, and the print statement will correctly report "Rob Ryan" even though the original person object is being mutated on another thread.

因此,如果您正在处理值类型和闭包,则值类型可以跨线程共享,除非您显式使用捕获列表(或等效的东西)以享受值语义(即根据需要复制对象).

So, if you are dealing with value types and closures, value types can be shared across threads unless you explicitly use capture list (or something equivalent) in order to enjoy value semantics (i.e. copying the object as needed).

这篇关于Apple 对多线程引用和值类型的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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