Re: [请益] 为什么 Go 会红?

楼主: oopFoo (3d)   2018-04-04 10:45:34
※ 引述《LinuxKernel (Linus Torvalds)》之铭言:
: 不敢说会写,只能说略懂,以下简单分享。
: 先说觉得好的地方
: 1. 对 non-blocking 的封装
: 大家都知道在 C/C++ 底下要写出高并发性能的 server 很多细节是很麻烦的
: 你可能说写 server 不就开个 socket 搭配 fork 多么直观
: 但现实是要达到好的性能就需要 select、epoll、kqueue 甚至 thread pool 之类的东西
: 但 non-blocking 的世界毕竟不是那么直观
: Go 强的地方就在于他让你用很直观的 blocking 写法
: 一个 client 进来你也就像 fork 一样给他开个 goroutine
: 但实际上它底层 runtime 就是用 epoll/kqueue 帮你做掉
: 也帮你做这些 lightweight thread 的 scheduling
: 当然 C/C++ 的世界也有类似的 library 可以办到,像是 Boost.Fiber
: 但 Go 里面用 goroutine 搭配 channel 感觉就是优雅了些
: 不过 goroutine 还是有可能踩到坑的
: 例如 goroutine 就永远卡在那边,这点可以去了解一下它 scheduling 的实作
goroutine应该说是preemptible blocking. epoll是io的部份,跟goroutine
无关。
Boost.fiber 是coroutine. 打个比喻就是win3.1的 cooperative multitasking.
goroutine 是 win95 的preemptive multitasking.完全不同等级的概念。
goroutine应该是模仿Erlang的process. channel就是message passing.
简单来讲golang就是better c + gc + concurrency.
goroutine 其实是golang的最重要功能。写过Multithread的人都知道,
尽量不要share memory. Lock carfully(avoid 2 locks if possible),
wait as little as possible.
Multithread是very HARD(tm)的,bug非常难找,真的极少人写的好。
写过Erlang的人都知道, concurrency is SIMPLE. golang也是
同样的.
为什么goroutine is SIMPLE, multithread is HARD. 重点就是channel,
(Message Passing). 只要不要share data. concurrency就简单了。
我们都知道Moore's Law is dead. 以后multi-core只会越来越多,
cpu的frequency不会增加太多。Concurrency programming只会越来越重要,
Erlang证明它的concurrency programming model work. Golang 是
google想办法把concurrency programming 带到大家能用的语言上来。
我觉得golang用的人会越来越多。它其实是真的c的后继语言。
大大推荐。
-补充一下
作者: dreamnook (亚龙)   2018-04-04 10:56:00
multi thread真的很头痛 碰到表皮就不敢再深入xD
作者: pokman (vern)   2018-04-04 13:13:00
那rust呢?
作者: foodordertw (foororderTW)   2018-04-04 13:50:00
你没讲到golang的scheduler的核心你应该要讲的是multithread并没有解决io blockinggoroutine则是原生的解决lock/io blocking
作者: rodion (r-kan/reminder)   2018-04-04 14:55:00
听起来跟python/js的async-io差不多?
作者: remmurds (Stronghold)   2018-04-04 15:15:00
https://github.com/ksimka/go-is-not-good/golang 所有的缺点都整理在这里了
作者: MineCola (MineCola)   2018-04-04 17:49:00
看到楼上连结里面某一条戳中笑点:un-googlable name
作者: ssadow (ssadow)   2018-04-04 19:46:00
写multi thread超好玩的 怎么大家都不喜欢而且goroutine不就是thread pool吗 还是multi thread啊
作者: soheadsome (师大狗鼻哥)   2018-04-04 20:12:00
gorountine是有调教过的thread 他的stack是可以动态调整如果是普通thread 他的stack就跟普通function差不多顶多多了local storage
作者: alex70266 (小眼)   2018-04-04 20:23:00
写multi thread 很好玩啊,debug 的时候就不好玩了XD
作者: snaketsai (さいでんし)   2018-04-05 06:30:00
goroutine会被preempt schedule ??刚刚看了runtime/proc.go的sysmon跟retake() ...这作法拿来讲preemption个人觉得实在有点微妙...
作者: badyy (nick)   2018-04-05 16:44:00
他的preempt schedule 是coopertive的
作者: PeacockLiu (小书生)   2018-04-06 00:40:00
曾经用C写了multithread,debug了半个月.....
作者: qmailtw (爱八卦的主管滚远点)   2018-04-12 12:36:00
硬件变快了,大家觉的牺牲ㄧ点效能来换工作效率看了这么长的文章,请问goroutine 和 coroutine 有什么不同?

Links booklink

Contact Us: admin [ a t ] ucptt.com