From e6e08d59bb9afdfe32fbaef1c8249181c159ee78 Mon Sep 17 00:00:00 2001 From: Luc Street Date: Sun, 7 Oct 2018 17:59:20 -0700 Subject: [PATCH] Basic threading for rust --- sheets/_rust/threads | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sheets/_rust/threads diff --git a/sheets/_rust/threads b/sheets/_rust/threads new file mode 100644 index 0000000..a43e24f --- /dev/null +++ b/sheets/_rust/threads @@ -0,0 +1,28 @@ +// Basic threading +use std::thread; + +fn main() { + // Spawn a new thread to do some work + let t = thread::spawn(|| { + println!("In a new thread!"); + }); + + // 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.