2018-10-08 00:59:20 +00:00
|
|
|
// Basic threading
|
|
|
|
use std::thread;
|
2018-10-08 10:12:32 +00:00
|
|
|
//
|
2018-10-08 00:59:20 +00:00
|
|
|
fn main() {
|
|
|
|
// Spawn a new thread to do some work
|
|
|
|
let t = thread::spawn(|| {
|
|
|
|
println!("In a new thread!");
|
|
|
|
});
|
2018-10-08 10:12:32 +00:00
|
|
|
|
2018-10-08 00:59:20 +00:00
|
|
|
// Wait for execution of spawned thread to join back up with main thread
|
|
|
|
let result = t.join();
|
|
|
|
|
|
|
|
// Create a bunch of threads, stored in a Vec
|
|
|
|
let mut threads = vec![];
|
|
|
|
for i in 0..10 {
|
|
|
|
threads.push(thread::spawn(|| {
|
|
|
|
println!("Doing some work!");
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all of those to finish
|
|
|
|
for t in threads {
|
|
|
|
let result = t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check out rayon https://docs.rs/rayon/ for more advanced tools like parallel
|
|
|
|
// iterators and sort algorithms.
|