Skip to content

Instantly share code, notes, and snippets.

@inetic
Created December 24, 2015 13:33
Show Gist options
  • Save inetic/f1c2a5cdb703fbbff7eb to your computer and use it in GitHub Desktop.
Save inetic/f1c2a5cdb703fbbff7eb to your computer and use it in GitHub Desktop.
use std::io::prelude::*;
use std::fs::File;
use std::thread;
macro_rules! iotry {
($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("{:?}", e) })
}
fn writer(mut f: File) {
loop {
iotry!(f.write_all(b"a"));
thread::sleep_ms(2000);
}
}
fn reader(mut f: File) {
let mut buf = [0u8; 1];
loop {
let r = f.read(&mut buf);
println!("{:?}", r);
thread::sleep_ms(1000);
}
}
fn main() {
let f1 = iotry!(File::create("test.txt"));
let writer_jh = thread::spawn(|| writer(f1));
let f2 = iotry!(File::open("test.txt"));
let reader_jh = thread::spawn(|| reader(f2));
iotry!(writer_jh.join());
iotry!(reader_jh.join());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment