Skip to content

rust并发模型 #1

@zhangsoledad

Description

@zhangsoledad

引用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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions