-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconcurrency_spawn_lifetimes.rs
More file actions
48 lines (38 loc) · 1.14 KB
/
concurrency_spawn_lifetimes.rs
File metadata and controls
48 lines (38 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Non-`'static` references can't be shared across threads using `std::thread::spawn`.
//
// error-pattern: explicit lifetime required in the type of `glossary`
#![allow(dead_code)]
use std::io;
use std::collections::BTreeMap;
use std::thread::spawn;
type GigabyteMap = BTreeMap<String, String>;
fn split_vec_into_chunks<T>(mut vec: Vec<T>, nchunks: usize) -> Vec<Vec<T>> {
unimplemented!()
}
fn process_files(filenames: Vec<String>, glossary: &GigabyteMap)
-> io::Result<()>
{
unimplemented!()
}
fn process_files_in_parallel(filenames: Vec<String>,
glossary: &GigabyteMap)
-> io::Result<()>
{
// Divide the work into several chunks.
const NTHREADS: usize = 8;
let worklists = split_vec_into_chunks(filenames, NTHREADS);
// Fork: Spawn a thread to handle each chunk.
let mut thread_handles = vec![];
for worklist in worklists {
thread_handles.push(
spawn(move || process_files(worklist, glossary)) // error
);
}
// Join: Wait for all threads to finish.
for handle in thread_handles {
handle.join().unwrap()?;
}
Ok(())
}
fn main() {
}