Skip to content

Instantly share code, notes, and snippets.

@gavinb
Created February 18, 2014 04:29
Show Gist options
  • Save gavinb/9064658 to your computer and use it in GitHub Desktop.
Save gavinb/9064658 to your computer and use it in GitHub Desktop.
Demonstrates a bug in c84890. Under OS X 10.9, crashes in receive code with null pointer.
// Rust IO Test
// Based on samples presented in Rob Pike's Go Concurrency talk
use std::libc::funcs::posix88::unistd::sleep;
use std::rand::random;
use std::comm::{Chan, Port};
struct Message {
string: ~str,
wait_chan: Chan<bool>,
wait_port: Port<bool>,
}
fn fan_in(p1: Port<~Message>, p2: Port<~Message>) -> Port<~Message> {
let (port, chan) = Chan::<~Message>::new();
let c1 = chan.clone();
spawn(proc() for d in p1.iter() { c1.send(d); });
let c2 = chan.clone();
spawn(proc() for d in p2.iter() { c2.send(d); });
return port;
}
// Returns a port for strings
fn boring(msg: ~str) -> Port<~Message> {
let (wait_port, wait_chan) = Chan::<bool>::new();
let (port, chan) = Chan::<~Message>::new();
spawn( proc()
for i in range(0, 5) {
let m = ~Message {
string: format!("{} {}", msg, i),
wait_chan: wait_chan,
wait_port: wait_port,
};
chan.send(m);
unsafe {
sleep((random::<u8>() % 3) as u32);
}
wait_port.recv();
}
);
return port;
}
fn main() {
let port = fan_in(boring(~"Joe"), boring(~"Ann"));
for i in range(0, 5) {
let msg1 = port.recv(); println!("{}", msg1.string);
let msg2 = port.recv(); println!("{}", msg2.string);
msg1.wait_chan.send(true);
msg2.wait_chan.send(true);
}
println!("You're all boring; I'm leaving.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment