Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created April 15, 2015 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcrichton/4f11bf049f9cc157d6fd to your computer and use it in GitHub Desktop.
Save alexcrichton/4f11bf049f9cc157d6fd to your computer and use it in GitHub Desktop.
#![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