Created
February 20, 2015 06:43
-
-
Save Centril/d224c40ae7fd56d96dc8 to your computer and use it in GitHub Desktop.
Rust: scoped, accessing parent thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::thread::{self, JoinGuard}; | |
use std::option::Option; | |
use std::marker::Send; | |
fn callback(v: usize) -> usize { v * 2 } | |
type CB = Fn(usize) -> usize + Sync + Send; | |
type Callback = Box<CB>; | |
struct S<'a> { | |
cb: Callback, | |
pub join_guard: Option<JoinGuard<'a, usize>>, | |
} | |
unsafe impl<'a> Send for S<'a> {} | |
impl<'a> S<'a> { | |
fn new( cb: Callback ) -> Self { | |
let mut s = S { cb: cb, join_guard: None }; | |
s.join_guard = Some( thread::scoped( || { | |
(s.cb)(1337) | |
}) ); | |
return s; | |
} | |
} | |
fn main() { | |
let mut s = S::new( Box::new( callback ) ); | |
if let Some(jg) = s.join_guard { | |
let r = jg.join(); | |
println!("{}", r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results in: