Rust编程语言入门之无畏并发

这篇具有很好参考价值的文章主要介绍了Rust编程语言入门之无畏并发。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

无畏并发

并发

  • Concurrent:程序的不同部分之间独立的执行(并发)
  • Parallel:程序的不同部分同时运行(并行)
  • Rust无畏并发:允许你编写没有细微Bug的代码,并在不引入新Bug的情况下易于重构
  • 注意:本文中的”并发“泛指 concurrent 和 parallel

一、使用线程同时运行代码(多线程)

进程与线程

  • 在大部分OS里,代码运行在进程(process)中,OS同时管理多个进程。
  • 在你的程序里,各独立部分可以同时运行,运行这些独立部分的就是线程(thread)
  • 多线程运行:
    • 提升性能表现
    • 增加复杂性:无法保障各线程的执行顺序

多线程可导致的问题

  • 竞争状态,线程以不一致的顺序访问数据或资源
  • 死锁,两个线程彼此等待对方使用完所持有的资源,线程无法继续
  • 只在某些情况下发生的 Bug,很难可靠地复制现象和修复

实现线程的方式

  • 通过调用OS的API来创建线程:1:1模型
    • 需要较小的运行时
  • 语言自己实现的线程(绿色线程):M:N模型
    • 需要更大的运行时
  • Rust:需要权衡运行时的支持
  • Rust标准库仅提供1:1模型的线程

通过 spawn 创建新线程

  • 通过 thread::spawn 函数可以创建新线程:
    • 参数:一个闭包(在新线程里运行的代码)
➜ cd rust

~/rust
➜ cargo new thread_demo
     Created binary (application) `thread_demo` package

~/rust
➜ cd thread_demo

thread_demo on  master [?] via 🦀 1.67.1
➜ c # code .

thread_demo on  master [?] via 🦀 1.67.1
➜

  • thread::sleep 会导致当前线程暂停执行
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));  // 暂停 1 毫秒
    }
}

执行

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 
➜ cargo run            
   Compiling thread_demo v0.1.0 (/Users/qiaopengjun/rust/thread_demo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.65s
     Running `target/debug/thread_demo`
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!
hi number 5 from the spawned thread!

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 
➜ 

通过 join Handle 来等待所有线程的完成

  • thread::spawn 函数的返回值类型是 JoinHandle
  • JoinHandle 持有值的所有权
    • 调用其 join 方法,可以等待对应的其它线程的完成
  • join 方法:调用 handle 的join方法会阻止当前运行线程的执行,直到 handle 所表示的这些线程终结。
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));  // 暂停 1 毫秒
    }

    handle.join().unwrap();
}

执行

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 
➜ cargo run
   Compiling thread_demo v0.1.0 (/Users/qiaopengjun/rust/thread_demo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.75s
     Running `target/debug/thread_demo`
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 spawned thread!
hi number 4 from the main 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!

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 

等分线程执行完继续执行主线程

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));
        }
    });

    handle.join().unwrap();

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

运行

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 
➜ cargo run
   Compiling thread_demo v0.1.0 (/Users/qiaopengjun/rust/thread_demo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/thread_demo`
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 3 from the spawned 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!
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 4 from the main thread!

thread_demo on  master [?] is 📦 0.1.0 via 🦀 1.67.1 

使用 move 闭包

  • move 闭包通常和 thread::spawn 函数一起使用,它允许你使用其它线程的数据
  • 创建线程时,把值的所有权从一个线程转移到另一个线程
use std::thread;

fn main() {
  let v = vec![1, 2, 3];
  let handle = thread::spawn(|| { // 报错
    println!("Here's a vector: {:?}", v);
  });
  
  // drop(v);
  handle.join().unwrap();
}

修改后:文章来源地址https://www.toymoban.com/news/detail-418201.html

use std::thread;

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

二、使用消息传递来跨线程传递数据

消息传递

  • 一种很流行且能保证安全并发的技术就是:消息传递。
    • 线程(或 Actor)通过彼此发送消息(数据)来进行通信
  • Go 语言的名言:不要用共享内存来通信,要用通信来共享内存。
  • Rust:Channel(标准库提供)

Channel

  • Channel 包含: 发送端、接收端
  • 调用发送端的方法,发送数据
  • 接收端会检查和接收到达的数据
  • 如果发送端、接收端中任意一端被丢弃了,那么Channel 就”关闭“了

创建 Channel

  • 使用 mpsc::channel函数来创建 Channel
    • mpsc 表示 multiple producer,single consumer(多个生产者、一个消费者)
    • 返回一个 tuple(元组):里面元素分别是发送端、接收端
use std::sync::mpsc;
use std::thread;

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);
}

发送端的 send 方法

  • 参数:想要发送的数据
  • 返回:Result<T, E>
    • 如果有问题(例如接收端已经被丢弃),就返回一个错误

接收端的方法

  • recv 方法:阻止当前线程执行,直到 Channel 中有值被送来
    • 一旦有值收到,就返回 Result<T, E>
    • 当发送端关闭,就会收到一个错误
  • try_recv 方法:不会阻塞,
    • 立即返回 Result<T, E>:
      • 有数据达到:返回 Ok,里面包含着数据
      • 否则,返回错误
    • 通常会使用循环调用来检查 try_recv 的结果

Channel 和所有权转移

  • 所有权在消息传递中非常重要:能帮你编写安全、并发的代码
use std::sync::mpsc;
use std::thread;

fn main() {
  let (tx, rx) = mpsc::channel();
  
  thread::spawn(move || {
    let val = String::from("hi");
    tx.send(val).unwrap();
    println!("val is {}", val)  // 报错 借用了移动的值
  });
  
  let received = rx.recv().unwrap();
  println!("Got: {}", received);
}

发送多个值,看到接收者在等待

use std::sync::mpsc;
use std::thread;

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_millis(1));
    }  
  });
  
  for received in rx {
    println!("Got: {}", received);
  }
}

通过克隆创建多个发送者

use std::sync::mpsc;
use std::thread;
use std::time::Duration;

fn main() {
  let (tx, rx) = mpsc::channel();
  
  let tx1 = mpsc::Sender::clone(&tx);
  thread::spawn(move || {
    let vals = vec![
      String::from("1: hi"),
      String::from("1: from"),
      String::from("1: the"),
      String::from("1: thread"),
    ];
    
    for val in vals {
      tx1.send(val).unwrap();
      thread::sleep(Duration::from_millis(1));
    }  
  });
   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_millis(1));
    }  
  });
  
  for received in rx {
    println!("Got: {}", received);
  }
}

三、共享状态的并发

使用共享来实现并发

  • Go 语言的名言:不要用共享内存来通信,要用通信来共享内存。
  • Rust支持通过共享状态来实现并发。
  • Channel 类似单所有权:一旦将值的所有权转移至 Channel,就无法使用它了
  • 共享内存并发类似多所有权:多个线程可以同时访问同一块内存

使用 Mutex 来每次只允许一个线程来访问数据

  • Mutex 是 mutual exclusion(互斥锁)的简写
  • 在同一时刻,Mutex 只允许一个线程来访问某些数据
  • 想要访问数据:
    • 线程必须首先获取互斥锁(lock)
      • lock 数据结构是 mutex 的一部分,它能跟踪谁对数据拥有独占访问权
    • mutex 通常被描述为:通过锁定系统来保护它所持有的数据

Mutex 的两条规则

  • 在使用数据之前,必须尝试获取锁(lock)。
  • 使用完 mutex 所保护的数据,必须对数据进行解锁,以便其它线程可以获取锁。

Mutex<T> 的 API

  • 通过 Mutex::new(数据) 来创建 Mutex<T>
    • Mutex<T>是一个智能指针
  • 访问数据前,通过 lock 方法来获取锁
    • 会阻塞当前线程
    • lock 可能会失败
    • 返回的是 MutexGuard(智能指针,实现了 Deref 和 Drop)
use std::sync::Mutex;

fn main() {
  let m = Mutex::new(5);
  
  {
    let mut num = m.lock().unwrap();
    *num = 6;
  }
  
  println!("m = {:?}", m);
}

多线程共享 Mutex<T>

use std::sync::Mutex;
use std::thread;

fn main() {
  let counter = Mutex::new(0);
  let mut handles = vec![];
  
  for _ in 0..10 {
     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());
}

多线程的多重所有权

use std::sync::Mutex;
use std::thread;
use std::rc::Rc;

fn main() {
  let counter = Rc::new(Mutex::new(0));
  let mut handles = vec![];
  
  for _ in 0..10 {
    let counter = Rc::clone(&counter);
    let handle = thread::spawn(move || {  // 报错 rc 只能用于单线程
       let mut num = counter.lock().unwrap();
       
       *num += 1;
    });
    handles.push(handle);
  }
  
  for handle in handles {
    handle.join().unwrap();
  }
  
  println!("Result: {}", *counter.lock().unwrap());
}

使用 Arc<T>来进行原子引用计数

  • Arc<T>Rc<T>类似,它可以用于并发情景
    • A:atomic,原子的
  • 为什么所有的基础类型都不是原子的,为什么标准库类型不默认使用 Arc<T>
    • 需要性能作为代价
  • Arc<T>Rc<T> 的API是相同的
use std::sync::{Mutex, Arc};
use std::thread;

fn main() {
  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();
  }
  
  println!("Result: {}", *counter.lock().unwrap());
}

RefCell<T>/Rc<T> vs Muter<T>/Arc<T>

  • Mutex<T>提供了内部可变性,和 Cell 家族一样
  • 我们使用 RefCell<T>来改变 Rc<T>里面的内容
  • 我们使用 Mutex<T> 来改变 Arc<T> 里面的内容
  • 注意:Mutex<T> 有死锁风险

四、通过 Send 和 Sync Trait 来扩展并发

Send 和 Sync trait

  • Rust 语言的并发特性较少,目前讲的并发特性都来自标准库(而不是语言本身)
  • 无需局限于标准库的并发,可以自己实现并发
  • 但在Rust语言中有两个并发概念:
    • std::marker::Sync 和 std::marker::Send 这两个trait

Send:允许线程间转移所有权

  • 实现 Send trait 的类型可在线程间转移所有权
  • Rust中几乎所有的类型都实现了 Send
    • Rc<T> 没有实现 Send,它只用于单线程情景
  • 任何完全由Send 类型组成的类型也被标记为 Send
  • 除了原始指针之外,几乎所有的基础类型都是 Send

Sync:允许从多线程访问

  • 实现Sync的类型可以安全的被多个线程引用
  • 也就是说:如果T是Sync,那么 &T 就是 Send
    • 引用可以被安全的送往另一个线程
  • 基础类型都是 Sync
  • 完全由 Sync 类型组成的类型也是 Sync
    • 但,Rc<T>不是 Sync 的
    • RefCell<T>Cell<T>家族也不是 Sync的
    • 而,Mutex<T>是Sync的

手动来实现 Send 和 Sync 是不安全的

到了这里,关于Rust编程语言入门之无畏并发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Rust编程语言入门之模式匹配

    模式是Rust中的一种特殊语法,用于匹配复杂和简单类型的结构 将模式与匹配表达式和其他构造结合使用,可以更好地控制程序的控制流 模式由以下元素(的一些组合)组成: 字面值 解构的数组、enum、struct 和 tuple 变量 通配符 占位符 想要使用模式,需要将其与某个值进行

    2023年04月22日
    浏览(28)
  • Rust编程语言入门之高级特性

    不安全 Rust 高级 Trait 高级 类型 高级函数和闭包 宏 隐藏着第二个语言,它没有强制内存安全保证:Unsafe Rust(不安全的 Rust) 和普通的 Rust 一样,但提供了额外的“超能力” Unsafe Rust 存在的原因: 静态分析是保守的。 使用 Unsafe Rust:我知道自己在做什么,并承担相应风险

    2023年04月24日
    浏览(28)
  • Rust编程语言入门之函数式语言特性:-迭代器和闭包

    闭包(closures) 迭代器(iterators) 优化改善 12 章的实例项目 讨论闭包和迭代器的运行时性能 闭包:可以捕获其所在环境的匿名函数。 闭包: 是匿名函数 保存为变量、作为参数 可在一个地方创建闭包,然后在另一个上下文中调用闭包来完成运算 可从其定义的作用域捕获值

    2023年04月08日
    浏览(23)
  • Rust编程语言入门之cargo、crates.io

    通过 release profile 来自定义构建 在https://crates.io/上发布库 通过 workspaces 组织大工程 从 https://crates.io/来安装库 使用自定义命令扩展 cargo release profile: 是预定义的 可自定义:可使用不同的配置,对代码编译拥有更多的控制 每个 profile 的配置都独立于其它的 profile cargo 主要的

    2023年04月09日
    浏览(18)
  • Rust编程语言入门之最后的项目:多线程 Web 服务器

    在 socket 上监听 TCP 连接 解析少量的 HTTP 请求 创建一个合适的 HTTP 响应 使用线程池改进服务器的吞吐量 优雅的停机和清理 注意:并不是最佳实践 创建项目 main.rs 文件 修改一: 修改二: 修改三: 修改四: 修改五: hello.html 文件 404.html 文件 单线程Web服务器 开启线程 lib.r

    2023年04月25日
    浏览(32)
  • Rust 是一种面向系统编程语言 主要被设计用来解决执行速度、安全性、并发性和可靠性等方面的问题 Rust From First Principles: Building a Minimal Rust

    作者:禅与计算机程序设计艺术 Rust 是一种面向系统编程语言,主要被设计用来解决执行速度、安全性、并发性和可靠性等方面的问题。相比于其他语言来说,它拥有以下优点: 高性能: Rust 的运行时是单线程的,但是拥有基于垃圾收集(GC)的自动内存管理机制,使得在开

    2024年02月07日
    浏览(37)
  • 【Rust】Rust学习 第十六章无畏并发

    安全且高效的处理并发编程是 Rust 的另一个主要目标。 并发编程( Concurrent programming ),代表程序的不同部分相互独立的执行,而 并行编程( parallel programming )代表程序不同部分于同时执行 ,这两个概念随着计算机越来越多的利用多处理器的优势时显得愈发重要。由于历

    2024年02月12日
    浏览(22)
  • GO语言网络编程(并发编程)并发介绍,Goroutine

    进程和线程 并发和并行 协程和线程 协程:独立的栈空间,共享堆空间,调度由用户自己控制,本质上有点类似于用户级线程,这些用户级线程的调度也是自己实现的。 线程:一个线程上可以跑多个协程,协程是轻量级的线程。 goroutine 只是由官方实现的超级\\\"线程池\\\"。 每个

    2024年02月09日
    浏览(24)
  • GO语言网络编程(并发编程)Channel

    1.1.1 Channel 单纯地将函数并发执行是没有意义的。函数与函数间需要交换数据才能体现并发执行函数的意义。 虽然可以使用共享内存进行数据交换,但是共享内存在不同的goroutine中容易发生竞态问题。为了保证数据交换的正确性,必须使用互斥量对内存进行加锁,这种做法势

    2024年02月09日
    浏览(36)
  • GO语言网络编程(并发编程)select

    1.1.1 select多路复用 在某些场景下我们需要同时从多个通道接收数据。通道在接收数据时,如果没有数据可以接收将会发生阻塞。你也许会写出如下代码使用遍历的方式来实现: 这种方式虽然可以实现从多个通道接收值的需求,但是运行性能会差很多。为了应对这种场景,G

    2024年02月09日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包