Rust 多线程编程的实现

 更新时间:2023年12月07日 11:39:32   作者:int8  
在rust中,多线程编程不算困难,但是也需要留心和别的编程语言中不同的地方,本文主要介绍了Rust 多线程编程的实现,感兴趣的可以了解一下

一个进程一定有一个主线程,主线程之外创建出来的线程称为子线程
多线程编程,其实就是在主线程之外创建子线程,让子线程和主线程并发运行,完成各自的任务。
Rust语言支持多线程编程。

Rust语言标准库中的 std::thread 模块用于多线程编程。
std::thread 提供很很多方法用于创建线程、管理线程和结束线程。

一、创建线程

使用std::thread::spawn()方法创建一个线程。

pub fn spawn<F, T>(f: F) -> JoinHandle<T>

参数 f 是一个闭包,是线程要执行的代码。

范例

use std::thread; // 导入线程模块
use std::time::Duration; // 导入时间模块
fn main() {
     //创建一个新线程
     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));
     }
}
编译运行结果如下
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the spawned thread!
hi number 4 from the main thread!

咦,执行结果好像出错了? 是吗?
当主线程执行结束,那么就会自动关闭创建出来的子线程。
上面的代码,我们调用 thread::sleep() 函数强制线程休眠一段时间,这就允许不同的线程交替执行。
虽然某个线程休眠时会自动让出cpu,但并不保证其它线程会执行。这取决于操作系统如何调度线程。
这个范例的输出结果是随机的,主线程一旦执行完成程序就会自动退出,不会继续等待子线程。这就是子线程的输出结果不全的原因。

二、让主线程等待子线程

默认情况下,主线程并不会等待子线程执行完毕。为了避免这种情况,我们可以让主线程等待子线程执行完毕然后再继续执行。

Rust标准库提供了 join() 方法用于把子线程加入主线程等待队列。

spawn<F, T>(f: F) -> JoinHandle<T>

范例

use std::thread;
use std::time::Duration;
fn main() {
     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));
     }
     handle.join().unwrap();
}
编译运行结果如下
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 2 from the main thread!
hi number 3 from the spawned thread!
hi number 3 from the main thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!

从输出结果来看,主线程和子线程交替执行。
主线程等待子线程执行完毕是因为调用了 join() 方法。

三、move强制所有权迁移

这是一个经常遇到的情况:
实例

use std::thread;
fn main() {
    let s = "hello";
   
    let handle = thread::spawn(|| {
        println!("{}", s);
    });

    handle.join().unwrap();
}

在子线程中尝试使用当前函数的资源,这一定是错误的!因为所有权机制禁止这种危险情况的产生,它将破坏所有权机制销毁资源的一定性。我们可以使用闭包的move关键字来处理:
实例

use std::thread;
fn main() {
    let s = "hello";
   
    let handle = thread::spawn(move || {
        println!("{}", s);
    });

    handle.join().unwrap();
}

四、消息传递

使用通道传递消息,通道有两部分组成,一个发送者(transmitter)和一个接收者(receiver)。
std::sync::mpsc包含了消息传递的方法:
实例

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();
    });
    let received = rx.recv().unwrap();
    println!("Got: {}", received);
}
运行结果:
Got: hi

子线程获得了主线程的发送者tx,并调用了它的send方法发送了一个字符串,然后主线程就通过对应的接收者rx接收到了。

到此这篇关于Rust 多线程编程的实现的文章就介绍到这了,更多相关Rust 多线程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Windows系统下安装Rust环境超详细教程

    Windows系统下安装Rust环境超详细教程

    这篇文章主要介绍了如何在Windows系统上安装mingw64和Rust,mingw64是一个轻便的C语言编译环境,可以替代Rust默认使用的Visual Studio,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2025-02-02
  • Rust中的关联类型总结

    Rust中的关联类型总结

    关联类型是定义通用trait的一种机制。它允许在trait中定义一个或多个占位符类型,这些类型将在trait的实现中具体化。文中有详细示例代码供参考,需要的朋友可以阅读一下
    2023-05-05
  • Rust中的内部可变性与RefCell<T>详解

    Rust中的内部可变性与RefCell<T>详解

    内部可变性允许在不可变引用中修改内部数据,通过RefCell在运行时检查借用规则,适用于Mock对象和多所有权的可变性场景,结合Rc和RefCell实现多所有者共享并修改数据,但仅适用于单线程
    2025-02-02
  • 一步到位,教你如何在Windows成功安装Rust

    一步到位,教你如何在Windows成功安装Rust

    一步到位:轻松学会在Windows上安装Rust!想快速掌握Rust编程语言?别再为复杂教程头疼!这份指南将手把手带你顺利完成Windows平台上的Rust安装全过程,从此编码之旅更加顺畅无阻,立即阅读,开始你的Rust编程旅程吧!
    2024-01-01
  • Rust字符串匹配Rabin-Karp算法详解

    Rust字符串匹配Rabin-Karp算法详解

    Rabin-Karp算法也可以叫 Karp-Rabin 算法,它是用来解决多模式串匹配问题的,它的实现方式有点与众不同,首先是计算两个字符串的哈希值,然后通过比较这两个哈希值的大小来判断是否出现匹配,本文详细介绍了字符串匹配Rabin-Karp算法,需要的朋友可以参考下
    2023-05-05
  • Rust 编程语言中的所有权ownership详解

    Rust 编程语言中的所有权ownership详解

    这篇文章主要介绍了Rust 编程语言中的所有权ownership详解的相关资料,需要的朋友可以参考下
    2023-02-02
  • rust将bitmap位图文件另存为png格式的方法

    rust将bitmap位图文件另存为png格式的方法

    通过添加依赖,转换函数和单元测试操作步骤来解决将bitmap位图文件另存为png格式文件,本文通过实例代码给大家介绍的非常详细,对rust bitmap位另存为png格式的操作方法感兴趣的朋友一起看看吧
    2024-03-03
  • Rust 连接 PostgreSQL 数据库的详细过程

    Rust 连接 PostgreSQL 数据库的详细过程

    这篇文章主要介绍了Rust 连接 PostgreSQL 数据库的完整代码,本文图文实例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • Rust中::和.的区别解析

    Rust中::和.的区别解析

    Rust中的::和.是两种常用的操作符,分别用于访问命名空间中的成员和实例的字段或方法,感兴趣的朋友跟随小编一起看看吧
    2024-11-11
  • rust多样化错误处理(从零学习)

    rust多样化错误处理(从零学习)

    一个优秀的项目,错误处理的优雅性是至关重要的,而rust,anyhow creat是绕不过去的一个,今天我们来研究下,怎么使用它,帮助我们写出更优雅的代码,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2023-11-11

最新评论