Why build concurrency on the ideas of CSP?
為什么要基于 CSP 的思想來構(gòu)建并發(fā)火俄?
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.
隨著時間的推移,并發(fā)和多線程編程已經(jīng)被認為是困難的适瓦。我們認為,一部分是由于復雜的設(shè)計(例如 pthreads) 犹菇,另一部分是由于過分強調(diào)低級細節(jié)(例如互斥體、條件變量和內(nèi)存屏障)芽卿。更高級別的接口可以使代碼更簡單胳搞,即使在幕后仍然存在互斥體等。
overemphasis
n. 過分的強調(diào);
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.
為并發(fā)提供高級語言支持的最成功的模型之一來自 Hoare 的通信順序進程 (CSP)肌毅。Occam 和 Erlang 是源自 CSP 的兩種著名語言。Go 的并發(fā)原語源自家族樹的不同部分悬而,其主要貢獻是將通道的概念作為一等對象。幾種早期語言的經(jīng)驗表明笨奠,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 是讓并發(fā)變得易于使用的一部分。這個想法已經(jīng)存在了一段時間蔚袍,即將獨立執(zhí)行的函數(shù)(協(xié)程)復用到一組線程上。當協(xié)程阻塞時,例如通過調(diào)用阻塞系統(tǒng)調(diào)用晋辆,運行時會自動將同一操作系統(tǒng)線程上的其他協(xié)程移動到不同的可運行線程渠脉,這樣它們就不會被阻塞瓶佳。程序員看不到這些,這就是重點涩哟。我們稱之為 goroutine 的結(jié)果可能非常便宜:除了堆棧內(nèi)存(只有幾千字節(jié))之外,它們幾乎沒有任何開銷贴彼。
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 的運行時使用可調(diào)整大小的有界堆棧。新創(chuàng)建的 Goroutine 被賦予幾千字節(jié)威鹿,這幾乎總是足夠的。如果不是忽你,運行時會自動增加(和縮小)用于存儲堆棧的內(nèi)存科雳,從而允許許多 goroutine 駐留在適量的內(nèi)存中。每個函數(shù)調(diào)用的 CPU 開銷平均約為 3 個廉價指令脓杉。在同一地址空間中創(chuàng)建數(shù)十萬個 goroutine 是很實用的。如果 goroutine 只是線程球散,那么系統(tǒng)資源就會以更少的數(shù)量耗盡。
minted
adj. 嶄新的蕉堰;新制作的;剛完成的;
instructions
n. 操作指南;用法說明;指示;命令( instruction的名詞復數(shù) );(計算機的)指令;教導;教誨;
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.
經(jīng)過長時間的討論丑婿,我們決定map的典型使用不需要從多個 goroutine 進行安全訪問没卸,并且在需要安全訪問的情況下秒旋,map可能是已經(jīng)同步的某些較大數(shù)據(jù)結(jié)構(gòu)或計算的一部分。因此迁筛,要求所有map操作獲取互斥鎖會減慢大多數(shù)程序的速度煤蚌,并增加少數(shù)程序的安全性细卧。然而,這不是一個容易的決定贪庙,因為這意味著不受控制的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更新止邮。當需要時,例如托管不受信任的程序時导披,該實現(xiàn)可以互鎖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.
僅當發(fā)生更新時map訪問才是不安全的撩匕。只要所有 goroutine 都只是讀取(在map中查找元素滑沧,包括使用for 、range迭代它)滓技, 喝不通過分配元素或執(zhí)行刪除來更改map棚潦,那么它們在不同步的情況下并發(fā)訪問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使用的輔助手段丸边,該語言的某些實現(xiàn)包含特殊檢查,當并發(fā)執(zhí)行不安全地修改映射時妹窖,該檢查會在運行時自動報告。
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.
人們經(jīng)常建議改進該語言—— 郵件列表 包含此類討論的豐富歷史——但這些更改很少被接受判没。
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 是一個開源項目,但該語言和庫受到兼容性承諾的保護澄峰,該承諾可以防止破壞現(xiàn)有程序的更改,至少在源代碼級別如此(程序可能需要偶爾重新編譯以保持最新狀態(tài))俏竞。如果您的提案違反了 Go 1 規(guī)范,我們甚至無法接受這個想法魂毁,無論其優(yōu)點如何。Go 未來的主要版本可能與 Go 1 不兼容席楚,但關(guān)于該主題的討論才剛剛開始疚漆,有一點是肯定的:在此過程中引入的此類不兼容性非常少酣胀。此外娶聘,兼容性承諾鼓勵我們?yōu)榕f程序提供一條自動前進路徑,以便在出現(xiàn)這種情況時進行適應(yīng)丸升。
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 規(guī)范兼容狡耻,它也可能不符合 Go 的設(shè)計目標精神。《Google 的 Go:軟件工程服務(wù)中的語言設(shè)計》一文 解釋了 Go 的起源及其設(shè)計背后的動機岭皂。