Created
April 15, 2015 17:57
-
-
Save alexcrichton/4f11bf049f9cc157d6fd to your computer and use it in GitHub Desktop.
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
#![feature(scoped)] | |
use std::thread; | |
use std::sync::mpsc::{channel, Sender, Receiver}; | |
fn main() { | |
let (tx1, rx1) = channel(); | |
let (tx2, rx2) = channel(); | |
let _b1 = Bomb1(tx1, rx2); | |
let b = Box::new(4); | |
foo(tx2, rx1, &b); | |
} | |
fn foo(done: Sender<()>, start: Receiver<()>, val: &i32) { | |
enum Item<'a> { | |
Thread(thread::JoinGuard<'a, ()>), | |
Bomb(Bomb2), | |
} | |
let mut v = Vec::new(); | |
v.push(Item::Bomb(Bomb2)); | |
v.push(Item::Thread(thread::scoped(move || { | |
drop(start.recv()); | |
println!("value at {:p} = {}", val, val); | |
drop(done.send(())); | |
}))); | |
} | |
struct Bomb1(Sender<()>, Receiver<()>); | |
impl Drop for Bomb1 { | |
fn drop(&mut self) { | |
println!("letting the child go"); | |
drop(self.0.send(())); | |
drop(self.1.recv()); | |
println!("returning from main thread"); | |
} | |
} | |
struct Bomb2; | |
impl Drop for Bomb2 { | |
fn drop(&mut self) { | |
panic!("panicking on main thread"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment