Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust并发模型 #1

Open
zhangsoledad opened this issue Oct 8, 2017 · 0 comments
Open

rust并发模型 #1

zhangsoledad opened this issue Oct 8, 2017 · 0 comments

Comments

@zhangsoledad
Copy link
Owner

引用Rob Pike的经典描述:

Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.

并发是同一时间应对(dealing with)多件事的能力;
并行是同一时间做(doing)多件事的能力;

并发编程的概念越来越广泛的收到关注,主要驱动力就在于如今我们正处于“多核危机”。CPU的单核性能一直没有重大的提升突破,目前CPU的发展模式变成了堆核和如何有效的堆核。

为了顺应这种趋势,并发编程也就越显重要。
8152114324

线程和锁
线程和锁模型是对底层硬件运行过程的形式化,

几乎所有的编程语言都对线程和锁提供了支持,rust也不例外,这种模型的优点在于非常简单直接,使用起来没有限制,但也是缺点所在,编程语言没有提供足够的帮助,使得程序容易出错,也要面临死锁的问题。

channel

Don't communicate by sharing memory; share memory by communicating

出了传统的锁之外,rust也提供了CSP模型的channel用于线程间通信。
缺失select。https://doc.rust-lang.org/stable/std/macro.select.html

原子类型 CAS
原子类型无锁, CAS,不会死锁,问题:swap 调用时,其他线程修改了原子变量的值,swap重试,swap必须无副作用。

https://aturon.github.io/blog/2015/08/27/epoch/
https://github.com/crossbeam-rs/crossbeam

线程 stack 保存挂起状态,linux 2MB,切换 数钱纳秒,还与stack 大小相关。

线程池 CPU密集,是线程复用。 IO型,堵塞。

事件驱动,future
https://docs.rs/futures/0.1.16/futures/
CPS变换
https://github.com/carllerche/tokio-serde-json/blob/master/src/lib.rs

https://github.com/alexcrichton/futures-await

coroutine
rust-lang/rust#43076

Repository owner locked and limited conversation to collaborators Oct 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant