Can you specify a non-static lifetime for threads? [duplicate]

心已入冬 提交于 2021-02-05 09:14:26

问题


Here's a toy example of my problem:

use std::sync::{Arc, Mutex};

fn operate_in_chunks(vec: &mut Vec<f32>) {
    let chunk_size = 10;
    let mutex_vec: Arc<Mutex<&mut Vec<f32>>> = Arc::new(Mutex::new(vec));

    let handles = Vec::new();

    for chunk in 0..vec.len() / chunk_size {
        handles.push(std::thread::spawn(move || {
            operate(mutex_vec, chunk);
        }));
    }

    for i in 0..handles.len() {
        handles[i].join().unwrap();
    }
}

fn operate(mutex_vec: Arc<Mutex<&mut Vec<f32>>>, chunk: usize) {}

I'd like to do some work on the passed-in struct, split among a number of threads, and then join them all back together before returning.

The error I get says:

error[E0621]: explicit lifetime required in the type of `vec`
  --> src/lib.rs:10:22
   |
3  | fn operate_in_chunks(vec: &mut Vec<f32>) {
   |                           ------------- help: add explicit lifetime `'static` to the type of `vec`: `&'static mut std::vec::Vec<f32>`
...
10 |         handles.push(std::thread::spawn(move || {
   |                      ^^^^^^^^^^^^^^^^^^ lifetime `'static` required

I understand what it's complaining about: if the threads may have a 'static lifetime, and they reference vec, vec must have a 'static lifetime. However, my use-case should in theory be possible: I want to guarantee that the threads don't have a 'static lifetime, as they're all joined before the function returns, in which case I shouldn't need a 'static' lifetime onvec`.

Does Rust have a way of articulating this - unifying the lifetime of the threads with the lifetime of vec - or are all threads always 'static no matter what?


回答1:


Rust actually used to allow scoped threads, which allow non-static lifetimes for any data passed. However, the API was found to be unsound and they were removed around two years ago. Fortunately, crossbeam, a crate, implements scoped threads with a different API allowing you to use this functionality safely. A sample from crossbeam's documentation is here:

let array = [1, 2, 3];

crossbeam::scope(|scope| {
    for i in &array {
        scope.spawn(move || {
            println!("element: {}", i);
        });
    }
});


来源:https://stackoverflow.com/questions/57643084/can-you-specify-a-non-static-lifetime-for-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!