并發(fā)編程指南(一):并發(fā)與應用設計

原文: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)建和維護它所使用的任何線程相關的大部分成本廊驼。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末腾夯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蔬充,更是在濱河造成了極大的恐慌,老刑警劉巖班利,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饥漫,死亡現(xiàn)場離奇詭異,居然都是意外死亡罗标,警方通過查閱死者的電腦和手機庸队,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闯割,“玉大人彻消,你說我怎么就攤上這事≈胬” “怎么了宾尚?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長谢澈。 經(jīng)常有香客問我煌贴,道長吩案,這世上最難降的妖魔是什么影钉? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮签财,結果婚禮上敬鬓,老公的妹妹穿的比我還像新娘淹朋。我一直安慰自己,他們只是感情好钉答,可當我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布础芍。 她就那樣靜靜地躺著,像睡著了一般数尿。 火紅的嫁衣襯著肌膚如雪者甲。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天砌创,我揣著相機與錄音虏缸,去河邊找鬼鲫懒。 笑死,一個胖子當著我的面吹牛刽辙,可吹牛的內容都是我干的窥岩。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼宰缤,長吁一口氣:“原來是場噩夢啊……” “哼颂翼!你這毒婦竟也來了?” 一聲冷哼從身側響起慨灭,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤朦乏,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后氧骤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呻疹,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡筹陵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年刽锤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片朦佩。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡并思,死狀恐怖,靈堂內的尸體忽然破棺而出语稠,到底是詐尸還是另有隱情宋彼,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布仙畦,位于F島的核電站宙暇,受9級特大地震影響,放射性物質發(fā)生泄漏议泵。R本人自食惡果不足惜占贫,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望先口。 院中可真熱鬧型奥,春花似錦、人聲如沸碉京。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谐宙。三九已至烫葬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背搭综。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工垢箕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人兑巾。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓条获,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蒋歌。 傳聞我的和親對象是個殘疾皇子帅掘,可洞房花燭夜當晚...
    茶點故事閱讀 42,786評論 2 345

推薦閱讀更多精彩內容

  • 有一天 你會離開父母 獨自打拼 有一天 你會離開朋友 各奔東西 有一天 你會離開愛人 獨守寂寞 你不知道 原來你始...
    塵默閱讀 327評論 0 5
  • 2017.5.18 星期四 晴 親子日記(25) 今天下班去貿易城給老公買了幾件背心。就先讓郭家誠媽媽接著...
    于澤媽媽閱讀 171評論 0 2