Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 中关于并发的一切:第一部分 — 当前 #1739

Merged
merged 6 commits into from
Jun 28, 2017

Conversation

Abysssea
Copy link
Contributor

@Abysssea Abysssea commented Jun 7, 2017

抱歉延迟了这么久!
这篇翻译的比较粗糙,�有不准确的地方请大佬们严厉指责!

@Feximin
Copy link
Contributor

Feximin commented Jun 20, 2017

校对认领 @sqrthree

1 similar comment
@zhangqicode
Copy link
Contributor

校对认领 @sqrthree

@linhe0x0
Copy link
Member

@Feximin @zhangqippp 好的,辛苦两位尽快哈~ 这篇文章时间有点长了。

@linhe0x0
Copy link
Member

@Feximin @zhangqippp 两位不要忘了校对对对对对

Copy link
Contributor

@Feximin Feximin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeepMissea @sqrthree 校对完毕,,DeepMissea大佬翻译的非常专业,膜拜。


In the first part of this series, we’ll take a look at what we have at our disposal with Swift 3, covering everything from Foundation locks, threads and timers to the language guarantees and the recently improved Grand Central Dispatch and Operation Queues.
在本系列教程的第一部分,我们会介绍 Swift 3 提供的功能,涵盖一切,从基础锁、线程和计时器,到语言守护和最近提升的 GCD 和操作队列。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『提升的』=->『改善的』


Even if they are available on every platform where Swift is available, the functions and primitives from the pthread library will not be discussed here, since for all of them, higher level alternatives exist.
The NSTimer class will also not be described here, take a look [here](/swiftbites/nstimer-in-swift-3/) for info on how to use it with Swift 3.
即使 Swift 可以在每个平台使用,关于 pthread 库的函数和原语也不会在这里讨论,因为对于每个平台,都有更高级的方案。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『即使 Swift 可以在每个平台使用,关于 pthread 库的函数和原语也不会在这里讨论』=->『即使pthread 库的函数和原语可以在任一个运行Swift的平台上使用,我们也不会在这里讨论』


This second article will be completely speculative and its main goal will be to give you an introduction to these subjects so that you’ll be able to participate to the, likely heated, discussions that will define how concurrency will be handled in the future releases of Swift.
这是本系列下一篇文章讨论的内容,我们会讨论一些其他语言实现的替代方法和范式实现,而在 Swift 中他们是如何实现的。并且我们会分析一些当前已经可以使用的开源的实现,如 Actor 范式,Go 的 CSP 通道,软件事务内存等在当前版本 Swift 中的功能。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『并且我们会分析一些当前已经可以使用的开源的实现,如 Actor 范式,Go 的 CSP 通道,软件事务内存等在当前版本 Swift 中的功能』=->『并且我们会分析一些用当前版本Swift完成的开源实现,这些实现中我们可以使用Actor 范式,Go 的 CSP 通道,软件事务内存等特性』


First of all, you could ask, even if Apple’s platform and frameworks use threads, why should you introduce them in your applications?
首先,你可能会问,即使 Apple 的平台和框架使用了线程,为什么要在应用程序中引入它们呢?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉作者想表达的意思是:虽然Apple 的平台和框架使用了线程,但是我为什么要在自己的应用中引入它们呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

�嗯,有道理。


- **Task groups separation**: Threads can be used to modularize your application from the point of view of execution flow, different threads can be used to execute in a predictable manner a group of task of the same type, isolating them from the execution flow of other parts of your program, making it easier to reason about the current state of your application.
- **任务组分离**: 线程能从执行流程的角度,模块化你的程序。不同的线程用可预测方式,执行一组相同的任务,把他们与你程序的其他执行流程部分隔离,这样你会更容易理解当前程序的状态。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『当前程序的状态』=->『程序当前的状态』

@@ -535,9 +545,9 @@ print("Gone!")

```

This is the first time we need to specify more than one attribute, but as you can see, you can just add multiple attributes with an array if needed.
这是我们第一次需要制定多个属性,但就如你所见,如果需要,你可以添加一个含有多个属性的数组。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『你可以添加一个含有多个属性的数组』=->『你可以用一个数组添加多个属性』


Execution of jobs can also be suspended or resumed temporarily with methods inherited from `DispatchObject`:
也可以使用集成自 `DispatchObject` 的方法暂停或恢复执行的工作:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『集成自』=->『继承自』

@@ -730,7 +740,7 @@ workItem.perform()

```

And WorkItems also provide other useful methods, like `notify` that as it did with groups allows to perform a closure on a specific queue on completion:
WorkItems 也提供其他很有用的方法,比如 `notify`,与组一样,允许在完成时在指定的队列上执行闭包。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

允许在一个指定的队列完成时执行一个闭包


Threads `wait` on a semaphore when the counter, decremented every time the semaphore is acquired, reaches 0.
信号量上的 `wait`,每当计数器递减时,信号量是可被线程获取的,直到为计数器值为 0。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

线程在信号量上’wait‘,直到那个每当信号量被获取时值都减小的计数器的值为0


I hope that these two articles will be a good introduction to the world of concurrency and that they will help you understand and participate to the discussions that will take place on the swift-evolution mailing list when the community will start considering what to introduce in, let’s hope, Swift 5.
第二部分将重点介绍下一步可能在语言中出现的处理并发的“原生”功能,而不需要借助外部库。现在已经有一些有意思的开源实现的范例。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通过目前的一些开源实现来讲述几个有意思的范例。

@linhe0x0
Copy link
Member

@zhangqippp 进度如何了呀?

Copy link
Contributor

@zhangqicode zhangqicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeepMissea , @sqrthree , 校对完毕,久等了,不好意思


If you plan to perform tasks concurrently and when you’ll need to deal with the resulting race conditions, your only option is to use external libraries like libDispatch or the synchronization primitives offered by Foundation or the OS.
如果你计划异步执行任务,并且需要处理由此产生的竞争条件时,你唯一的选择就是使用 Foundation 提供的外部库,比如 libDispatch,或者 OS 提供的同步原语。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你唯一的选择就是使用 Foundation 提供的外部库,比如 libDispatch,或者 OS 提供的同步原语。

你唯一的选择就是使用外部库,比如 libDispatch,或者 Foundation 和 OS 提供的同步原语。


Even if they are available on every platform where Swift is available, the functions and primitives from the pthread library will not be discussed here, since for all of them, higher level alternatives exist.
The NSTimer class will also not be described here, take a look [here](/swiftbites/nstimer-in-swift-3/) for info on how to use it with Swift 3.
即使 Swift 可以在每个平台使用,关于 pthread 库的函数和原语也不会在这里讨论,因为对于每个平台,都有更高级的方案。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

即使 Swift 可以在每个平台使用

即使可以在 Swift 支持的所有平台使用


This second article will be completely speculative and its main goal will be to give you an introduction to these subjects so that you’ll be able to participate to the, likely heated, discussions that will define how concurrency will be handled in the future releases of Swift.
这是本系列下一篇文章讨论的内容,我们会讨论一些其他语言实现的替代方法和范式实现,而在 Swift 中他们是如何实现的。并且我们会分析一些当前已经可以使用的开源的实现,如 Actor 范式,Go 的 CSP 通道,软件事务内存等在当前版本 Swift 中的功能。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

而在 Swift 中他们是如何实现的

和在 Swift 中它们是如何实现的


#### *The playgrounds for this and other articles are available from [GitHub](https://github.com/uraimo/Swift-Playgrounds) or [Zipped](/archives/2017-05-07-ConcurrencyInSwift.playground.zip).* ####
第二篇文章将会完全是投机的,它主要的目的是为你介绍这些主题,以便你以后可以参与到更热烈讨论当中,而这些讨论将会定义未来 Swift 版本的并发是怎么处理的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

投机的

推测性的


Computing platforms with more than one processors or processors with more than one hardware execution core have been around for a few decades and concept like *thread* and *process* are even older than that.
具有多个处理器或者多个硬件执行核心的计算平台已经存在了几十年,而像 **thread** 和 **process** 这样的概念甚至更久。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

具有多个处理器或者多个硬件执行核心

具有多个处理器或者多核处理器


For those that are not already familiar with this API, the Grand Central Dispatch (GCD) is a queue based API that allows to execute closures on workers pools.
对于不熟悉他们的 API 的人来说,GCD 是一种基于队列的 API,允许在工作池上执行闭包。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于不熟悉他们的 API 的人来说

对于不熟悉这个 API 的人来说

@@ -498,9 +508,9 @@ DispatchQueue.global(qos: .background).async {

```

Closures can also be executed after a specific delay, Swift 3 finally allows to specify in a more comfortable way the desired time interval with the utility enum `DispatchTimeInterval` that allows to compose intervals using these four time units: `.seconds(Int)`, `.milliseconds(Int)`, `.microseconds(Int)` and `.nanoseconds(Int)`.
闭包也可以在一个特定的延迟执行,Swift 3 最终以一种更舒适的方式指定这个时间间隔,那就是使用 `DispatchTimeInterval` 工具枚举,它允许使用这四个时间工具组成间隔:`.seconds(Int)``.milliseconds(Int)``.microseconds(Int)` `.nanoseconds(Int)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

它允许使用这四个时间工具组成间隔

它允许使用这四个时间单位组成间隔

@@ -509,7 +519,7 @@ globalDefault.asyncAfter(deadline: .now() + .seconds(5)) {

```

If you need to execute multiple iteration of the same closure concurrently (like you used to to with the *dispatch_apply*) you can use the `concurrentPerform(iterations:execute:)` method, but beware, these closure will be executed concurrently *if possible in the context of the current queue*, so remember to always enclose a call to this method in a sync or async call running on a queue that support concurrency.
如果你需要多次并发执行相同的闭包(就像你以前用 **dispatch_apply** 一样),你可以使用 `concurrentPerform(iterations:execute:)` 方法,但请注意,**如果在当前队列的上下文中**,这些闭包会并发执行,所以记得,在支持并发的队列中运行的同步或异步调用中,始终会调用此方法。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果在当前队列的上下文中

如果在当前队列的上下文中可能的话

@@ -509,7 +519,7 @@ globalDefault.asyncAfter(deadline: .now() + .seconds(5)) {

```

If you need to execute multiple iteration of the same closure concurrently (like you used to to with the *dispatch_apply*) you can use the `concurrentPerform(iterations:execute:)` method, but beware, these closure will be executed concurrently *if possible in the context of the current queue*, so remember to always enclose a call to this method in a sync or async call running on a queue that support concurrency.
如果你需要多次并发执行相同的闭包(就像你以前用 **dispatch_apply** 一样),你可以使用 `concurrentPerform(iterations:execute:)` 方法,但请注意,**如果在当前队列的上下文中**,这些闭包会并发执行,所以记得,在支持并发的队列中运行的同步或异步调用中,始终会调用此方法。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在支持并发的队列中运行的同步或异步调用中,始终会调用此方法

始终应该在支持并发的队列中同步或异步地调用此方法

@@ -885,9 +895,9 @@ queue.addOperation{

```

We can also create a *Block Operation* object and configure it before adding it to the queue and if needed we can also add more than one closure to this kind of operations.
我们也可以创建一个**阻塞操作**对象,然后在加入队列之前配置它,如有需要,我们也可以添加多个这种操作的闭包。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们也可以添加多个这种操作的闭包

我们也可以向这种操作添加多个闭包

@linhe0x0
Copy link
Member

@DeepMissea 两位校对者都已经校对好了~ 可以来根据校对意见进行调整了哈 ┏ (゜ω゜)=☞

Copy link
Member

@linhe0x0 linhe0x0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有几个小问题麻烦调整下哈。

- [Using Queues](#using-queues)
- [Barriers](#barriers)
- [Singletons and Dispatch_once](#singletons-and-dispatch_once)
- [OSAtomic 你在哪里?](#OSAtomic 你在哪里?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

- [调度队列](#调度队列)
- [使用队列](#使用队列)
- [屏障](#屏障)
- [Singletons Dispatch_once](#Singletons 和 Dispatch_once)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@Abysssea
Copy link
Contributor Author

@sqrthree 搞定啦

@linhe0x0 linhe0x0 merged commit cab6405 into xitu:master Jun 28, 2017
@linhe0x0
Copy link
Member

@DeepMissea 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

@Abysssea
Copy link
Contributor Author

@Abysssea Abysssea deleted the translate branch July 16, 2017 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants