并发编程指南(一):并发与应用设计

原文: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.

尽管线程已出现多年,而且还会继续它们的使用,但是它们并不能解决以可扩展的方式来执行多任务这个通用的问题。对于线程,创建可伸缩解决方案的重担就完全落在开发者的肩上了。你必须要决定有多少线程需要创建,而且随着系统状况的改变还要动态调整线程数量。另一个问题是,应用程序承担与创建和维护它所使用的任何线程相关的大部分成本。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容

  • 有一天 你会离开父母 独自打拼 有一天 你会离开朋友 各奔东西 有一天 你会离开爱人 独守寂寞 你不知道 原来你始...
    塵默阅读 325评论 0 5
  • 2017.5.18 星期四 晴 亲子日记(25) 今天下班去贸易城给老公买了几件背心。就先让郭家诚妈妈接着...
    于泽妈妈阅读 170评论 0 2