diff --git a/examples/integrate-thread.rs b/examples/integrate-thread.rs new file mode 100644 index 0000000..3465e5d --- /dev/null +++ b/examples/integrate-thread.rs @@ -0,0 +1,74 @@ +#![feature(async_await)] + +use async_std::task; + +use std::{thread,time}; +use futures::channel::oneshot; + +struct AsyncHandle { + handle: thread::JoinHandle, + notifier: oneshot::Receiver<()>, +} + +impl AsyncHandle { + fn thread(&self) -> &std::thread::Thread { + self.handle.thread() + } + + async fn join(self) -> std::thread::Result { + // ignore results, the join handle will propagate panics + let _ = self.notifier.await; + self.handle.join() + } +} + + +fn spawn(f: F) -> AsyncHandle +where + F: FnOnce() -> T, + F: Send + 'static, + T: Send + 'static, +{ + let (sender, receiver) = oneshot::channel::<()>(); + + let thread_handle = thread::spawn(move || { + let res = f(); + sender.send(()).unwrap(); + res + }); + + AsyncHandle { + handle: thread_handle, + notifier: receiver + } +} + +fn main() { + let thread_handle = spawn(move || { + thread::sleep(time::Duration::from_millis(1000)); + String::from("Finished") + }); + + task::block_on(async move { + println!("waiting for thread 1"); + let thread_result = thread_handle.join().await; + match thread_result { + Ok(s) => println!("Result: {}", s), + Err(e) => println!("Error: {:?}", e), + } + }); + + let thread_handle = spawn(move || { + panic!("aaah!"); + String::from("Finished!") + }); + + task::block_on(async move { + println!("waiting for thread 2"); + let thread_result = thread_handle.join().await; + match thread_result { + Ok(s) => println!("Result: {}", s), + Err(e) => println!("Error: {:?}", e), + } + }); +} \ No newline at end of file