rust学习-线程

Rust 标准库只提供了 1:1 线程模型

Rust 是较为底层的语言,如果愿意牺牲性能来换取抽象,以获得对线程运行更精细的控制及更低的上下文切换成本,使用实现了 M:N 线程模型的 crate

示例

复制代码
use std::thread;
use std::time::Duration;

fn main() {
    // 调用 thread::spawn 函数并传递一个闭包,来创建线程
    let handle = thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {} from the spawned thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 1..5 {
        println!("hi number {} from the main thread!", i);
        thread::sleep(Duration::from_millis(1));
    }

    // 等待结束
    // // `unwrap` 在接收到 `None` 时将返回 `panic`
    handle.join().unwrap();
}

在一个线程中使用另一个线程的数据

在参数列表前使用 move 关键字强制闭包获取其使用的环境值的所有权。

这个技巧在创建新线程将值的所有权从一个线程移动到另一个线程时最为实用

复制代码
use std::thread;

fn main() {
    let v = vec![1, 2, 3]
    
    let handle = thread::spawn(|| {
        // `v` is borrowed here
        // Rust 不知道这个新建线程会执行多久,所以无法知晓 v 的引用是否一直有效
        println!("Here's a vector: {:?}", v);
    });

	// 一个具有闭包的线程,尝试使用一个在主线程中被回收的引用 v
	// drop(v);

    handle.join().unwrap();
}

force the closure to take ownership of v (and any other referenced variables), use the move keyword

复制代码
use std::thread;

fn main() {
    let v = vec![1, 2, 3];

    // 增加 move 关键字,强制闭包获取其使用的值的所有权
    let handle = thread::spawn(move || {
        println!("Here's a vector: {:?}", v);
    });

    handle.join().unwrap();
    // value borrowed here after move
    println!("Here's a vector: {:?}", v); // 这里编译报错
}

use std::thread;

fn main() {
    let v = vec![1, 2, 3]
    
    let handle = thread::spawn(move || {
        // `v` is borrowed here
        println!("Here's a vector: {:?}", v);
    });

    handle.join().unwrap();
}

channel

mpsc:multiple producer, single consumer

基本用法

复制代码
use std::thread;
// 多发单收模型
// multiple producer, single consumer
use std::sync::mpsc;

fn main() {
	// tx 是发送端,rx是接收端
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let val = String::from("hi");
        tx.send(val).unwrap();
    });

	// 主线程阻塞
    let received = rx.recv().unwrap();
    println!("Got: {}", received);
}

try_recv 不会阻塞,相反它立刻返回一个 Result<T, E>:Ok 值包含可用的信息,而 Err 值代表此时没有任何消息

示例

复制代码
use std::thread;
use std::sync::mpsc;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let vals = vec![
            String::from("hi"),
            String::from("from"),
            String::from("the"),
            String::from("thread"),
        ];

        for val in vals {
            tx.send(val).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });

	// 打印出四个消息后退出
    for received in rx {
        println!("Got: {}", received);
    }
}

所有权被转移

复制代码
use std::thread;
use std::sync::mpsc;

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let val = String::from("hi");
        tx.send(val).unwrap();
        // 编译失败
        //  value borrowed here after move
        println!("val is {}", val);
    });

    let received = rx.recv().unwrap();
    println!("Got: {}", received);
}

多生产者

通过克隆通道的发送端来做到

类似于Go中的:"不要通过共享内存来通讯;而是通过通讯来共享内存

("Do not communicate by sharing memory; instead, share memory by communicating.")

复制代码
use std::thread;
use std::sync::mpsc;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::channel();

    let tx1 = tx.clone();
    thread::spawn(move || {
	let vals = vec![
	    String::from("hi"),
	    String::from("from"),
	    String::from("the"),
	    String::from("thread"),
	];

	for val in vals {
	    tx1.send(val).unwrap();
	    thread::sleep(Duration::from_secs(1));
	}
    });

    thread::spawn(move || {
	let vals = vec![
	    String::from("more"),
	    String::from("messages"),
	    String::from("for"),
	    String::from("you"),
	];

	for val in vals {
	    tx.send(val).unwrap();
	    thread::sleep(Duration::from_secs(1));
	}
    });

    for received in rx {
	println!("Got: {}", received);
    }

    println!("finish")
}

Mutex 互斥器

使用方式

复制代码
cat main.rs
use std::sync::Mutex;

fn main() {
    let m = Mutex::new(5);

    {
        // lock 调用 返回 一个叫做 MutexGuard 的智能指针
        // 这个智能指针实现了 Deref 来指向其内部数据
        // 也提供了一个 Drop 实现当 MutexGuard 离开作用域时自动释放锁
        let mut num = m.lock().unwrap();
        *num = 6;
    }

    // 打印内容如下 Mutex { data: 6, poisoned: false, .. }
    println!("m = {:?}", m);
}

糟糕例子

复制代码
use std::sync::Mutex;
use std::thread;

fn main() {
    let counter = Mutex::new(0);
    let mut handles = vec![];

    for _ in 0..10 {
        // value moved into closure here, in previous iteration of loop
        // 编译失败
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();

            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Result: {}", *counter.lock().unwrap());
}

解决方式-Arc

Arc 和 Rc 有着相同的 API

Arc 一个类似 Rc 并可以安全的用于并发环境的类型

字母 "a" 代表 原子性(atomic),所以这是一个原子引用计数

为什么不是所有标准库中的类型都默认使用 Arc 实现?

线程安全带有性能惩罚,只在必要时才为此买单

复制代码
use std::sync::{Mutex, Arc};
use std::thread;

fn main() {
    // counter 是不可变的,因为前面没有mut
    // 意味着Mutex<T> 提供了内部可变性
    // 就像使用 RefCell<T> 可以改变 Rc<T> 中的内容那样
    // 使用 Mutex<T> 来改变 Arc<T> 中的内容 
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

    for _ in 0..10 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();

            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

	// 打印结果为10
    println!("Result: {}", *counter.lock().unwrap());
}

Mutex 也有造成 死锁(deadlock) 的风险

使用 Sync 和 Send trait 的可扩展并发

语言本身对并发知之甚少。上述讨论的几乎所有内容都属于标准库,而不是语言本身的内容,可以编写自己的或使用别人编写的并发功能。

但有两个并发概念是内嵌于语言中的:std::marker 中的 Sync 和 Send trait

通过 Send 允许在线程间转移所有权

【Send 标记 trait】

表明类型的所有权可以在线程间传递

几乎所有的 Rust 类型都是Send 的,但是也有例外,比如Rc

如果克隆了 Rc 的值并尝试将克隆的所有权转移到另一个线程,这两个线程都可能同时更新引用计数,Rc 被实现为用于单线程场景,所以不用担心多线程下的引用计数问题。

糟糕例子

the trait Send is not implemented for Rc<Mutex<i32>>

复制代码
use std::rc::Rc;
use std::sync::Mutex;
use std::thread;

fn main() {
    let counter = Rc::new(Mutex::new(0));
    let mut handles = vec![];

    // Rc<Mutex<i32>>` cannot be sent between threads safely
    for _ in 0..10 {
        let counter = Rc::clone(&counter);
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();

            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Result: {}", *counter.lock().unwrap());
}

解决办法

使用原子引用计数 Arc

Sync 允许多线程访问

【Sync 标记 trait】

一个实现了 Sync 的类型可以安全的在多个线程中拥有其值的引用

对于任意类型 T,如果 &T(T 的引用)是 Send 的话 T 就是 Sync 的

其引用就可以安全的发送到另一个线程

Rc 不是 Sync的

RefCell 和 Cell 系列类型不是 Sync 的

RefCell 在运行时所进行的借用检查也不是线程安全的

Mutex 是 Sync 的,可以被用来在多线程中共享访问

相关推荐
世人万千丶7 小时前
参数管理_Flutter在鸿蒙平台路由参数最佳实践
学习·flutter·华为·harmonyos·鸿蒙
大模型码小白7 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
段一凡-华北理工大学9 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe10 小时前
C++——多态
开发语言·c++
遇乐的果园10 小时前
前端学习笔记-vue加载渲染优化
前端·笔记·学习
心平气和量大福大10 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
遇乐的果园11 小时前
前端学习笔记-vue状态管理优化
前端·笔记·学习
从零开始的代码生活_12 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸12 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
可乐奶茶sky12 小时前
AI Agent 学习
人工智能·学习