设计

2024-01-14  本文已影响0人  王老怪的开发笔记

Why build concurrency on the ideas of CSP?

为什么要基于 CSP 的思想来构建并发?

Concurrency and multi-threaded programming have over time developed a reputation for difficulty. We believe this is due partly to complex designs such as pthreads and partly to overemphasis on low-level details such as mutexes, condition variables, and memory barriers. Higher-level interfaces enable much simpler code, even if there are still mutexes and such under the covers.
随着时间的推移,并发和多线程编程已经被认为是困难的。我们认为,一部分是由于复杂的设计(例如 pthreads) ,另一部分是由于过分强调低级细节(例如互斥体、条件变量和内存屏障)。更高级别的接口可以使代码更简单,即使在幕后仍然存在互斥体等。

overemphasis
n. 过分的强调;

One of the most successful models for providing high-level linguistic support for concurrency comes from Hoare's Communicating Sequential Processes, or CSP. Occam and Erlang are two well known languages that stem from CSP. Go's concurrency primitives derive from a different part of the family tree whose main contribution is the powerful notion of channels as first class objects. Experience with several earlier languages has shown that the CSP model fits well into a procedural language framework.
为并发提供高级语言支持的最成功的模型之一来自 Hoare 的通信顺序进程 (CSP)。Occam 和 Erlang 是源自 CSP 的两种著名语言。Go 的并发原语源自家族树的不同部分,其主要贡献是将通道的概念作为一等对象。几种早期语言的经验表明,CSP 模型非常适合过程语言框架。

stem
n. 茎;高脚杯的脚;烟斗柄;词干;
v. 阻止;

Why goroutines instead of threads?

为什么使用 goroutine 而不是线程?

Goroutines are part of making concurrency easy to use. The idea, which has been around for a while, is to multiplex independently executing functions—coroutines—onto a set of threads. When a coroutine blocks, such as by calling a blocking system call, the run-time automatically moves other coroutines on the same operating system thread to a different, runnable thread so they won't be blocked. The programmer sees none of this, which is the point. The result, which we call goroutines, can be very cheap: they have little overhead beyond the memory for the stack, which is just a few kilobytes.
Goroutine 是让并发变得易于使用的一部分。这个想法已经存在了一段时间,即将独立执行的函数(协程)复用到一组线程上。当协程阻塞时,例如通过调用阻塞系统调用,运行时会自动将同一操作系统线程上的其他协程移动到不同的可运行线程,这样它们就不会被阻塞。程序员看不到这些,这就是重点。我们称之为 goroutine 的结果可能非常便宜:除了堆栈内存(只有几千字节)之外,它们几乎没有任何开销。

multiplex
n. 多厅影院,多剧场影剧院;多路,复用;

To make the stacks small, Go's run-time uses resizable, bounded stacks. A newly minted goroutine is given a few kilobytes, which is almost always enough. When it isn't, the run-time grows (and shrinks) the memory for storing the stack automatically, allowing many goroutines to live in a modest amount of memory. The CPU overhead averages about three cheap instructions per function call. It is practical to create hundreds of thousands of goroutines in the same address space. If goroutines were just threads, system resources would run out at a much smaller number.
为了使堆栈变小,Go 的运行时使用可调整大小的有界堆栈。新创建的 Goroutine 被赋予几千字节,这几乎总是足够的。如果不是,运行时会自动增加(和缩小)用于存储堆栈的内存,从而允许许多 goroutine 驻留在适量的内存中。每个函数调用的 CPU 开销平均约为 3 个廉价指令。在同一地址空间中创建数十万个 goroutine 是很实用的。如果 goroutine 只是线程,那么系统资源就会以更少的数量耗尽。

minted
adj. 崭新的;新制作的;刚完成的;

instructions
n. 操作指南;用法说明;指示;命令( instruction的名词复数 );(计算机的)指令;教导;教诲;
adj. 说明用法的,操作指南的;

Why are map operations not defined to be atomic?

为什么map操作没有定义为原子操作?
After long discussion it was decided that the typical use of maps did not require safe access from multiple goroutines, and in those cases where it did, the map was probably part of some larger data structure or computation that was already synchronized. Therefore requiring that all operations grab a mutex would slow down most programs and add safety to few. This was not an easy decision, however, since it means uncontrolled map access can crash the program.
经过长时间的讨论,我们决定map的典型使用不需要从多个 goroutine 进行安全访问,并且在需要安全访问的情况下,map可能是已经同步的某些较大数据结构或计算的一部分。因此,要求所有map操作获取互斥锁会减慢大多数程序的速度,并增加少数程序的安全性。然而,这不是一个容易的决定,因为这意味着不受控制的map访问可能会使程序崩溃。

The language does not preclude atomic map updates. When required, such as when hosting an untrusted program, the implementation could interlock map access.
该语言并不排除原子map更新。当需要时,例如托管不受信任的程序时,该实现可以互锁map访问。

preclude
vt. 阻止;排除;妨碍;

interlock
v. 互锁;连锁;

Map access is unsafe only when updates are occurring. As long as all goroutines are only reading—looking up elements in the map, including iterating through it using a for range loop—and not changing the map by assigning to elements or doing deletions, it is safe for them to access the map concurrently without synchronization.
仅当发生更新时map访问才是不安全的。只要所有 goroutine 都只是读取(在map中查找元素,包括使用for 、range迭代它), 喝不通过分配元素或执行删除来更改map,那么它们在不同步的情况下并发访问map是安全的。

As an aid to correct map use, some implementations of the language contain a special check that automatically reports at run time when a map is modified unsafely by concurrent execution.
作为正确map使用的辅助手段,该语言的某些实现包含特殊检查,当并发执行不安全地修改映射时,该检查会在运行时自动报告。

Will you accept my language change?

您能接受我的语言更改吗?

People often suggest improvements to the language—the mailing list contains a rich history of such discussions—but very few of these changes have been accepted.
人们经常建议改进该语言—— 邮件列表 包含此类讨论的丰富历史——但这些更改很少被接受。

Although Go is an open source project, the language and libraries are protected by a compatibility promise that prevents changes that break existing programs, at least at the source code level (programs may need to be recompiled occasionally to stay current). If your proposal violates the Go 1 specification we cannot even entertain the idea, regardless of its merit. A future major release of Go may be incompatible with Go 1, but discussions on that topic have only just begun and one thing is certain: there will be very few such incompatibilities introduced in the process. Moreover, the compatibility promise encourages us to provide an automatic path forward for old programs to adapt should that situation arise.
尽管 Go 是一个开源项目,但该语言和库受到兼容性承诺的保护,该承诺可以防止破坏现有程序的更改,至少在源代码级别如此(程序可能需要偶尔重新编译以保持最新状态)。如果您的提案违反了 Go 1 规范,我们甚至无法接受这个想法,无论其优点如何。Go 未来的主要版本可能与 Go 1 不兼容,但关于该主题的讨论才刚刚开始,有一点是肯定的:在此过程中引入的此类不兼容性非常少。此外,兼容性承诺鼓励我们为旧程序提供一条自动前进路径,以便在出现这种情况时进行适应。

compatibility
n. 并存,相容;兼容性;

Even if your proposal is compatible with the Go 1 spec, it might not be in the spirit of Go's design goals. The article Go at Google: Language Design in the Service of Software Engineering explains Go's origins and the motivation behind its design.
即使您的提案与 Go 1 规范兼容,它也可能不符合 Go 的设计目标精神。《Google 的 Go:软件工程服务中的语言设计》一文 解释了 Go 的起源及其设计背后的动机。

上一篇 下一篇

猜你喜欢

热点阅读