Achieving concurrency in Go

Golang 中的 concurrency。关于 thread 与 goroutine 的对比较为详细。

线程/ thread 协程 / goroutine
系统的线程是由内核管理,有硬件依赖 goroutines 通过 Go 运行时管理,没有硬件依赖
系统线程通常有 1-2 MB 大小的固定堆栈 goroutines 在最新版的 Go 中通常是 8kb 大小的堆栈
堆栈大小在编译时及被决定,并且不能增加 堆栈大小由运行时进行管理,通过释放和分配新的堆栈可以增加到 1GB
在线程间没有方便的通信媒介 goroutine 通过 channels 进行低延时通信 (read more).
线程具有唯一的身份. 通过线程ID(TID)辨别在进程中的每个线程 goroutine 并没有任何唯一身份识别. 因为 Go 没有线程本地存储 TLS(Thread Local Storage).
线程有非常显著的创建和销毁成本,创建时需要向系统申请大量资源,并在销毁时将资源返还 goroutines 则是在运行时进行创建和销毁,这些操作相比于线程成本很低,因为 Go 运行时为 goroutines 维护了一个线程池. 在这种情况下,系统并不知道 goroutines 的存在
线程预先预定好了,(read here). 在线程之间切换的成本非常高,因为调度器需要保存/恢复多余 50 个注册器和状态,当线程间发生高速切换时,这成本开销将会非常明显 goroutines 则被合作安排了 (read more). 当 goroutine 切换发生时, 只有 3 个注册器需要保存或恢复

Achieving concurrency in Go

comments powered by Disqus

Tags

abcs accept acid activemq affinity algorithm allocation android array async aws b+tree b-tree backoff benchmark best-practices bfs big-o bigquery bind bitcount blog break broker bubble buffer cache cap cert cgroups channel citus class classmethod cluster concurrency config consumer container cookie cors crawler cronjob csrf ctr data-science data-structure database datadog dataflow datascience decorator defer dfs distributed django dns docker double-shipping drf ecosia elastic-search enumerate epoll extra facebook-pixel financial-report flask flutter forward-proxy freelance frp garbage-collector gc gcp generator get gil git golang goroutine graphql ha handbook haproxy hash hash-slot hashring hashtable hpa http http-auth http-proxy http_proxy https index innodb instagram intergration interview ios javascript jinja2 jobboard jwt k8s kafka kibana kqueue label lambda layer4 layer7 lean levels.io linked-list linux list listen loadbalancer logs long-tail lru marketing master matplotlib memory merge metaclass metaprogramming metrics metrics-server microservices mitm model mq myisam mysql namespace nat netflix network-extension nginx nodejs nomad nosql npm oodesign openssl optimization orm pandas parallelism parkinglot patroni permission pg pipeline pixelme post postgresql postresql prefetch_related prerender process proxy proxycommand put pvm python queue rabbitmq rbac react-native rebase redis redis-cluster replication resource rest restfulapi retargeting retry revenue reverse-proxy rocketmq saas scaleable search-engine security select seo serverless service session set shadosocks shadowsocks shard sharding shell shopify sigint signal sigterm slack slave slow-query sniper sns socket socks5 source-code spa sql sqlalchemy sqs ssh ssl ssl-pinning stack startup state stateful stateless staticmethod string struct swift switch syscall system-design systemctl tcp tcp-proxy thread tmpreaper token traefik trustkit tunning type typeform udp variable vc vpn vuejs web web-development where yarn zset 削峰 宽索引 异步 窄索引 解耦 跨域 跳板机



Archives

2019 (137)
0001 (1)