编程语言当前并发思路比较
Andy erpingwu@gmail.com
2012/02/17-2013/07/25
肤浅地比较一下,感觉编程语言当前对并发(这里暂不讨论传统线程模式)的思路基本一致:
- 将大的分布式系统通过
消息通信
增强并发的概念缩小到进程内。 - 在
coroutine
方面则是将大而重的系统调度部分转为线程内轻量的自主调度。
go-lang
Do not communicate by sharing memory; instead, share memory by communicating.
http://golang.org/doc/effective_go.html#concurrency
rust-lang
Instead of manipulating shared storage, Rust tasks communicate with one another using a typed, asynchronous, simplex message-passing system.
http://doc.rust-lang.org/doc/rust.html#tasks
erlang
Processes communicate by sending and receiving messages … Message sending is asynchronous and safe, the message is guaranteed to eventually reach the recipient, provided that the recipient exists
http://www.erlang.org/doc/reference_manual/processes.html
scala
是深受erlang影响的 actor, 惊叹号都一样。Scala 2.10 改用 akka.actor。
Actors are very lightweight concurrent entities. They process messages asynchronously using an event-driven receive loop. Pattern matching against messages is a convenient way to express an actor's behavior.
http://docs.scala-lang.org/overviews/core/actors.html
http://akka.io/
c++
c++11 也对并发做了大量工作,但都是线程与Atomic,主要是支持了 memory order。 boost 库倒是有提供 Coroutine,同时 boost.asio 有一个例子 A single-threaded HTTP server implemented using stackless coroutines.
libprocess Library that provides an actor style message-passing programming model (in C++). 这个用在 mesos
python
cpython略过
pypy
stackless
Microthreads tasklets wrap functions allowing them to be launched as microthreads.
Channels channels can be used for bidirectional communication between tasklets.
greenlet
The “greenlet” package is a spin-off of Stackless, a version of CPython that supports micro-threads called “tasklets”. Tasklets run pseudo-concurrently (typically in a single or a few OS-level threads) and are synchronized with data exchanges on “channels”.