Can multiple threads share non-static data that is temporarily immutable (while the threads are running)?
For example, this code gives an error:
error[E0621]: explicit lifetime required in the type of
v
(line 4)
use std::sync::Arc;
use std::thread;
fn f(v : &Vec<i64>)
{
let n = v.len();
let a = Arc::new(v);
let mut ts = Vec::new();
for i in 0..n
{
let a_clone = a.clone();
let t = thread::spawn(move ||
{
println!("thread {}, v[{}] = {}", i, i, a_clone[i]);
});
ts.push(t);
}
for t in ts
{
t.join().unwrap();
}
}
fn g()
{
let mut v : Vec<i64> = vec![1, 2, 3, 4, 5];
f(&v);
v.push(6);
dbg!(v);
}
fn main()
{
g();
}
Can I massage it a bit so that it works?