原文: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.
在計算機的早期乳绕,計算機單位時間內能夠執(zhí)行的最大工作量是由CPU的時鐘速度決定的。但是甘穿,隨著技術的進步课梳、處理器設計變得更加緊湊风钻、發(fā)熱和其他物理因素的限制,處理器的時鐘速度開始到達了最大極限。而且捞魁,芯片制造商開始探索其他方法來增加他們的芯片總體性能在张。通過增加核心數(shù)用含,單個芯片美妙能夠執(zhí)行更多的指令,而不用增加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.
為了利用多核,計算機需要能夠同時做多件事情的軟件∥列保現(xiàn)在缸夹,多任務操作系統(tǒng)痪寻,如OS X or iOS,在任意給定的時間內都能有上百個或更多的程序在運行虽惭,因此橡类,在不同核心上調度每一個程序就因該成為可能。然而芽唇,這些程序大部分要么是系統(tǒng)守護進程顾画,要么是那些消耗非常少的處理時間的后臺程序。相反匆笤,對于單個應用程序來說研侣,實際需要的是一種能夠高效利用那些額外核心的方法。
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.
對于一個應用程序炮捧,傳統(tǒng)的使用多核的方法是創(chuàng)建多個線程庶诡。然而,隨著核心數(shù)的增加咆课,線程方案產生了很多問題末誓。最大的問題是線程代碼不能很好地擴展到任意數(shù)量的內核。你不能創(chuàng)建和內核數(shù)一樣多的線程书蚪,而且還希望程序能良好運行基显。您需要知道的是可以高效使用的核心數(shù)量,這對于應用程序自己計算是一個挑戰(zhàn)性的事情善炫。即使你成功做到獲取正確的核心數(shù)撩幽,為如此多的線程編程,還要讓它們高效運行并保證互相之間不起沖突箩艺,這也仍然是很有挑戰(zhàn)性窜醉。
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.
因此,為了總結這個問題艺谆,對于應用程序來說榨惰,就需要有一種方法來利用不同數(shù)據(jù)量的計算機核心。單個應用的可執(zhí)行工作數(shù)量也需要能夠動態(tài)擴展静汤,以適應不斷變化的系統(tǒng)狀況琅催。而且,這個解決方案必須要足夠簡單虫给,這樣就不會增加利用這些核心所需的工作量藤抡。好消息是:蘋果的操作系統(tǒng)提供了所有這些問題的解決方案,而且抹估,本章將介紹包含此解決方案的技術和可以利用代碼進行代碼設計的技術缠黍。
遠離線程 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.
盡管線程已出現(xiàn)多年,而且還會繼續(xù)它們的使用药蜻,但是它們并不能解決以可擴展的方式來執(zhí)行多任務這個通用的問題瓷式。對于線程替饿,創(chuàng)建可伸縮解決方案的重擔就完全落在開發(fā)者的肩上了。你必須要決定有多少線程需要創(chuàng)建贸典,而且隨著系統(tǒng)狀況的改變還要動態(tài)調整線程數(shù)量视卢。另一個問題是,應用程序承擔與創(chuàng)建和維護它所使用的任何線程相關的大部分成本廊驼。