原文:Concurrency Programming Guide
翻译: skycolor
Concurrency is the notion of multiple things happening at the same time. With the proliferation of multicore CPUs and the realization that the number of cores in each processor will only increase, software developers need new ways to take advantage of them. Although operating systems like OS X and iOS are capable of running multiple programs in parallel, most of those programs run in the background and perform tasks that require little continuous processor time. It is the current foreground application that both captures the user’s attention and keeps the computer busy. If an application has a lot of work to do but keeps only a fraction of the available cores occupied, those extra processing resources are wasted.
并发是一个这样的概念:多件事情在同时发生。随着多核CPU的激增,在每个处理器核心数只会逐渐增加的现实下,软件开发者需要新的方法来利用这些核。尽管操作系统(如OS X和iOS)都有能力并行运行多个程序,但是这些程序大部分都在后台,而且执行任务的时候也只需要很少的连续处理器时间。正是前台的应用程序吸引了用户的注意,并且让计算机忙碌。如果一个应用程序有很多工作要做,但是只让可利用的多核心中的一小部分核心工作,那么这些额外的处理资源就被浪费了。
In the past, introducing concurrency to an application required the creation of one or more additional threads. Unfortunately, writing threaded code is challenging. Threads are a low-level tool that must be managed manually. Given that the optimal number of threads for an application can change dynamically based on the current system load and the underlying hardware, implementing a correct threading solution becomes extremely difficult, if not impossible to achieve. In addition, the synchronization mechanisms typically used with threads add complexity and risk to software designs without any guarantees of improved performance.
在过去,在应用程序中使用并发要创建一个或多个附加的线程。不幸的是,写线程相关的代码是一个具有挑战性的事情。线程是一个低级工具,而且必须要手动管理。考虑到一个应用程序的最优线程数量是基于当前系统负载和底层硬件动态变化的,那么要实现一个正确线程解决方案就变得极其困难,也不是不可能实现。此外,通常与线程一起使用的同步机制也增加了复杂度,同时也让软件设计没有了性能提升的保证。
Both OS X and iOS adopt a more asynchronous approach to the execution of concurrent tasks than is traditionally found in thread-based systems and applications. Rather than creating threads directly, applications need only define specific tasks and then let the system perform them. By letting the system manage the threads, applications gain a level of scalability not possible with raw threads. Application developers also gain a simpler and more efficient programming model.
OS X和iOS都大量采用了异步的方式来实现并发任务,而不是基于线程的系统和应用程序中那样传统的做法。与其直接创建线程,应用程序不如只需定义指定任务,然后让系统去执行它们。通过让系统来管理线程,应用程序获得了一定的伸缩性而不是和原始线程打交道的可能。同时,应用开发者也获得了一个更加简洁高效的编程模型。
This document describes the technique and technologies you should be using to implement concurrency in your applications. The technologies described in this document are available in both OS X and iOS.
这篇文档中描述的技术与方法可以让你在应用中实现并发编程。文档中描述的这些方法在OS X和iOS中都可使用。
Organization of This Document
This document contains the following chapters:
Concurrency and Application Designintroduces the basics of asynchronous application design and the technologies for performing your custom tasks asynchronously.
Operation Queuesshows you how to encapsulate and perform tasks using Objective-C objects.
Dispatch Queuesshows you how to execute tasks concurrently in C-based applications.
Dispatch Sourcesshows you how to handle system events asynchronously.
Migrating Away from Threadsprovides tips and techniques for migrating your existing thread-based code over to use newer technologies.
This document also includes a glossary that defines relevant terms.
文档组织
这篇文档包含了如下章节:
1. 并发编程与应用设计:本章节介绍了异步应用设计的基本概念和异步执行自定义任务的方法。
2. 操作队列:向你展示了如何使用Objective-C对象封装和执行任务。
3. 派发队列:向你展示了如何在基于C的应用中并发执行任务。
4. 派发源:向你展示了如何异步处理系统事件。
5. 迁出线程:对于将现存的基于线程的代码转化为使用新技术的代码,本章节提供了提示和技术方案。
A Note About Terminology
Before entering into a discussion about concurrency, it is necessary to define some relevant terminology to prevent confusion. Developers who are more familiar with UNIX systems or older OS X technologies may find the terms “task”, “process”, and “thread” used somewhat differently in this document. This document uses these terms in the following way:
The term thread is used to refer to a separate path of execution for code. The underlying implementation for threads in OS X is based on the POSIX threads API.
The term process is used to refer to a running executable, which can encompass multiple threads.
The term task is used to refer to the abstract concept of work that needs to be performed.
For complete definitions of these and other key terms used by this document, see Glossary.
术语注解
在开始讨论并发编程之前,为了防止迷惑,有必要先定义一些相关的术语。熟悉UNIX或旧版本OS X系统的开发者可能会发现:文档中不同的使用了“task”, “process”和 “thread”这些术语。这篇文档用下面的方式使用这些术语:
#术语 thread 用来指独立的代码执行路径。OS X底层的线程实现是基于POSIX 线程 API。
#术语 process 用来指正在运行的可执行代码,它包含多个线程。
#术语 task 用来指需要被执行的工作的抽象概念。
这些术语完整的定义和文档中使用的其他术语参见术语表。
See Also
This document focuses on the preferred technologies for implementing concurrency in your applications and does not cover the use of threads. If you need information about using threads and other thread-related technologies, seeThreading Programming Guide.
其他参考
这篇文档聚焦在应用中实现并发编程的更好的技术,而没有关注线程的使用。如果你需要关于线程使用和其他线程相关的技术,可以参考线程编程指南 。