I'd like to share the storage
variable in the code below among threads. But obviously the code has a problem that it tries to move
storage multiple times (because of the loop) and if I removed the move
then storage
becomes short lived and code won't compile too.
Is there a safe way to achieve this?
use std::thread;
use std::sync::{Arc, Mutex};
use std::fs::File;
use std::io::prelude::*;
struct Storage {
handler: File,
}
fn main() {
let storage = Storage {
handler: File::open("foo.txt").unwrap(),
};
let storage = Arc::new(storage);
for _ in 0..30 {
let handle = thread::spawn(move || {
let stor = Arc::clone(&storage);
});
}
}