SHARE
TWEET

Untitled

a guest Sep 21st, 2019 99 in 7 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate num_cpus;
  2.  
  3. use futures::executor::ThreadPool;        // Thread pool to run everything on.
  4. use futures::executor::ThreadPoolBuilder; // Builder for ^
  5. use futures::task::SpawnExt;              // Trait we need in scope for
  6.                                           // ThreadPool::spawn_with_handle
  7. use std::{thread};
  8.  
  9.  
  10. fn main() -> std::io::Result<()> {
  11.     let mut pool = ThreadPoolBuilder::new().pool_size(8).create()?;
  12.  
  13.     // We cannot pass the thread pool into the async function as is due
  14.     // to borrow checker rules, so we "clone" it. This is just a shallow
  15.     // increment of an Arc, so is cheap.
  16.     let clone = pool.clone();
  17.  
  18.     println!("num cpus: {:?}",num_cpus::get());
  19.  
  20.     for n in 0..12{
  21.         //println!("scheduling {:?}",n);
  22.         let future = async {            
  23.             println!("executing threadid {:?}...", thread::current().id())
  24.         };
  25.  
  26.         pool.spawn_ok(future);
  27.     }
  28.     println!("finished scheduling futures");
  29.  
  30.  
  31.     Ok(())
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top