了解 Rob Pike 的图书焚烧示例

Understanding Rob Pike's book incineration example

在 Rob Pike 2012 年的演讲 Concurrency is not Parallelism (it's better) 中,他举了一个 运行ning 一群 gophers 一起焚烧一堆书的例子。

在幻灯片 15 (video 6:58) 上,三个 gophers 在工作:

  1. 一个人把一堆书装进手推车。
  2. 将装满的手推车移到焚化炉(然后将空手推车送回)。
  3. 一个人将书从手推车移入焚化炉。

slide 16,四个 gophers 正在一起解决同一个问题:

  1. 一个人把一堆书装进手推车(和以前一样)。
  2. 有人将装满的推车移到焚化炉。
  3. 将书从手推车移入焚化炉(和以前一样)。
  4. 有人把空车移回堆里。

Pike 说这会执行更多的工作,但会 运行 更快。为什么?看起来工作量相同,由更多的 gophers 分担。

假设携带 loaded 购物车的 gopher 在从购物车中取出书籍后必须执行某种清理或通信。现在他可以做到 ,而 另一只地鼠将 车运回去。因此,加载地鼠不必等待推车回来。

考虑推车往返。

在第一种情况下我们有:

  • 将装满的手推车移到焚化炉(然后返回空手推车)。

即:

  • Gopher 1:将装满的推车移至焚化炉。

  • Gopher 1:将空车移回堆中。

在第二种情况下我们有:

  • 有人将装满的推车移到焚化炉。

  • 一个人把空车移回堆里。

即:

  • Gopher 1:将装满的推车移至焚化炉。

  • Gopher 1:没有手推车走回堆里。

  • Gopher 2:将空车移回堆中。

  • Gopher 2:没有推车走回焚化炉。

虽然我们有额外的工作,但由于一些原因,例如劳动分工,它可能 运行 更快。

Of the Division of Labour

To take an example, therefore, from a very trifling manufacture, but one in which the division of labour has been very often taken notice of, the trade of a pin-maker: a workman not educated to this business (which the division of labour has rendered a distinct trade), nor acquainted with the use of the machinery employed in it (to the invention of which the same division of labour has probably given occasion), could scarce, perhaps, with his utmost industry, make one pin in a day, and certainly could not make twenty. But in the way in which this business is now carried on, not only the whole work is a peculiar trade, but it is divided into a number of branches, of which the greater part are likewise peculiar trades. One man draws out the wire; another straights it; a third cuts it; a fourth points it; a fifth grinds it at the top for receiving the head; to make the head requires two or three distinct operations; to put it on is a peculiar business; to whiten the pins is another; it is even a trade by itself to put them into the paper; and the important business of making a pin is, in this manner, divided into about eighteen distinct operations, which, in some manufactories, are all performed by distinct hands, though in others the same man will sometimes perform two or three of them. I have seen a small manufactory of this kind, where ten men only were employed, and where some of them consequently performed two or three distinct operations. But though they were very poor, and therefore but indifferently accommodated with the necessary machinery, they could, when they exerted themselves, make among them about twelve pounds of pins in a day. There are in a pound upwards of four thousand pins of a middling size. Those ten persons, therefore, could make among them upwards of forty-eight thousand pins in a day. Each person, therefore, making a tenth part of forty-eight thousand pins, might be considered as making four thousand eight hundred pins in a day. But if they had all wrought separately and independently, and without any of them having been educated to this peculiar business, they certainly could not each of them have made twenty, perhaps not one pin in a day; that is, certainly, not the two hundred and fortieth, perhaps not the four thousand eight hundredth, part of what they are at present capable of performing, in consequence of a proper division and combination of their different operations.

An Inquiry into the Nature and Causes of the Wealth of Nations, Adam Smith, 1776.

诀窍在于,不仅有一辆购物车,还有多辆购物车。一种被填满,一种被转移到焚化炉,在被清空到焚化炉中,一种被移出焚化炉。这意味着每个 gopher 应该(只要事情合理地工作并且每个任务的完成时间相对相似)总是很忙。