3 ms·
That Go concurrency is threads is the point of the article. It says this explicitly: "You can think of goroutines as threads, it's a fairly good mental model. T
by jules 8y ago
That Go concurrency is threads is the point of the article. It says this explicitly: "You can think of goroutines as threads, it's a fairly good mental model. They are truly cheap threads". If OS threads become as cheap as userland threads then the implementation of userland threads becomes unnecessary, but the conceptual model stays the same.
In other languages, asynchronous APIs return futures or have explicit callbacks, whereas synchronous APIs return the type directly, thus creating a type level distinction between asynchronous and synchronous code. Go's model eliminates that distinction even though it calls asynchronous OS APIs under the hood.
Go isn't the king of cheap threads, though. Some cilk style task parallelism implementations have figured out clever tricks to make threads and synchronisation even cheaper. They're so cheap in fact, that a recursive fibonacci function that spawns threads for its recursive calls is almost as fast as one that doesn't. This is achieved by spawning those threads lazily: if a core is idle it looks at the call stacks of other cores and retroactively spawns threads for some of the remaining work on those stacks. It steals that work by mutating that call stack in such a way that if that other core returns to the stack frame it will not perform that work itself, but it will use the result computed by the core that stole the work. This not only avoids thread spawning cost unless the parallelism is actually used, but it also avoids synchronisation cost, because synchronisation is only necessary if work was actually stolen. Cilk style task parallelism traditionally focuses on parallelism rather than concurrency, but there's no reason why the same implementation strategies couldn't work for concurrency. OS threads have no hope of beating this.
- lachlan-sneff 8y agoI've never heard of cilk before, but it seems fascinating. Is there any material I could read on how this is implemented?
- nelsonje 8y agoThis is the canonical Cilk paper: http://supertech.csail.mit.edu/papers/cilk5.pdf http://supertech.csail.mit.edu/papers/cilk5.pdf
- jules 8y agoHere is the paper describing the lazy thread spawning implementation: https://ece.umd.edu/~barua/ppopp164.pdf https://ece.umd.edu/~barua/ppopp164.pdf
- pcwalton 8y agoNote that there is a widely-used implementation of Cilk for Rust, known as Rayon, used in Firefox Quantum among other projects.
- fulafel 8y agoAfter a web search I read that Rayon is a word play on Cilk, but neither are dictionary words.. Would someone explain the word play in the names for us non-native speakers?
- loonyphoenix 8y agoRayon[1] is artificial silk. [1]: https://en.wikipedia.org/wiki/Rayon https://en.wikipedia.org/wiki/Rayon