并发编程指南(一):并发与应用设计
原文:Concurrency and Application Design
In the early days of computing, the maximum amount of work per unit of time that a computer could perform was determined by the clock speed of the CPU. But as technology advanced and processor designs became more compact, heat and other physical constraints started to limit the maximum clock speeds of processors. And so, chip manufacturers looked for other ways to increase the total performance of their chips. The solution they settled on was increasing the number of processor cores on each chip. By increasing the number of cores, a single chip could execute more instructions per second without increasing the CPU speed or changing the chip size or thermal characteristics. The only problem was how to take advantage of the extra cores.
在计算机的早期,计算机单位时间内能够执行的最大工作量是由CPU的时钟速度决定的。但是,随着技术的进步、处理器设计变得更加紧凑、发热和其他物理因素的限制,处理器的时钟速度开始到达了最大极限。而且,芯片制造商开始探索其他方法来增加他们的芯片总体性能。通过增加核心数,单个芯片美妙能够执行更多的指令,而不用增加CPU速度或者改变芯片尺寸或热力学特性。唯一的问题是如何利用这些额外的核心。
In order to take advantage of multiple cores, a computer needs software that can do multiple things simultaneously. For a modern, multitasking operating system like OS X or iOS, there can be a hundred or more programs running at any given time, so scheduling each program on a different core should be possible. However, most of these programs are either system daemons or background applications that consume very little real processing time. Instead, what is really needed is a way for individual applications to make use of the extra cores more effectively.
为了利用多核,计算机需要能够同时做多件事情的软件。现在,多任务操作系统,如OS X or iOS,在任意给定的时间内都能有上百个或更多的程序在运行,因此,在不同核心上调度每一个程序就因该成为可能。然而,这些程序大部分要么是系统守护进程,要么是那些消耗非常少的处理时间的后台程序。相反,对于单个应用程序来说,实际需要的是一种能够高效利用那些额外核心的方法。
The traditional way for an application to use multiple cores is to create multiple threads. However, as the number of cores increases, there are problems with threaded solutions. The biggest problem is that threaded code does not scale very well to arbitrary numbers of cores. You cannot create as many threads as there are cores and expect a program to run well. What you would need to know is the number of cores that can be used efficiently, which is a challenging thing for an application to compute on its own. Even if you manage to get the numbers correct, there is still the challenge of programming for so many threads, of making them run efficiently, and of keeping them from interfering with one another.
对于一个应用程序,传统的使用多核的方法是创建多个线程。然而,随着核心数的增加,线程方案产生了很多问题。最大的问题是线程代码不能很好地扩展到任意数量的内核。你不能创建和内核数一样多的线程,而且还希望程序能良好运行。您需要知道的是可以高效使用的核心数量,这对于应用程序自己计算是一个挑战性的事情。即使你成功做到获取正确的核心数,为如此多的线程编程,还要让它们高效运行并保证互相之间不起冲突,这也仍然是很有挑战性。
So, to summarize the problem, there needs to be a way for applications to take advantage of a variable number of computer cores. The amount of work performed by a single application also needs to be able to scale dynamically to accommodate changing system conditions. And the solution has to be simple enough so as to not increase the amount of work needed to take advantage of those cores. The good news is that Apple’s operating systems provide the solution to all of these problems, and this chapter takes a look at the technologies that comprise this solution and the design tweaks you can make to your code to take advantage of them.
因此,为了总结这个问题,对于应用程序来说,就需要有一种方法来利用不同数据量的计算机核心。单个应用的可执行工作数量也需要能够动态扩展,以适应不断变化的系统状况。而且,这个解决方案必须要足够简单,这样就不会增加利用这些核心所需的工作量。好消息是:苹果的操作系统提供了所有这些问题的解决方案,而且,本章将介绍包含此解决方案的技术和可以利用代码进行代码设计的技术。
远离线程 The Move Away from Threads
Although threads have been around for many years and continue to have their uses, they do not solve the general problem of executing multiple tasks in a scalable way. With threads, the burden of creating a scalable solution rests squarely on the shoulders of you, the developer. You have to decide how many threads to create and adjust that number dynamically as system conditions change. Another problem is that your application assumes most of the costs associated with creating and maintaining any threads it uses.
尽管线程已出现多年,而且还会继续它们的使用,但是它们并不能解决以可扩展的方式来执行多任务这个通用的问题。对于线程,创建可伸缩解决方案的重担就完全落在开发者的肩上了。你必须要决定有多少线程需要创建,而且随着系统状况的改变还要动态调整线程数量。另一个问题是,应用程序承担与创建和维护它所使用的任何线程相关的大部分成本。
明天继续!!!