Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Last active August 29, 2015 14:15
Show Gist options
  • Save alexcrichton/fdc28c0f4a69652c65d1 to your computer and use it in GitHub Desktop.
Save alexcrichton/fdc28c0f4a69652c65d1 to your computer and use it in GitHub Desktop.
use std::thread;
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
static BUF: &'static [u8] = &[1; 128 * 1024];
fn main() {
let l = TcpListener::bind("127.0.0.1:13566").unwrap();
let _t1 = thread::scoped(|| {
for a in l.incoming() {
let mut a = a.unwrap();
loop {
match a.write(BUF) {
Ok(..) => {}
Err(..) => break,
}
}
}
});
let _t1 = thread::scoped(|| {
let mut s = TcpStream::connect("127.0.0.1:13566").unwrap();
let mut b = [0; 64 * 1024];
loop {
// for b in b.iter_mut() { *b = 0; }
match s.read(&mut b) {
Ok(n) => { CNT.fetch_add(n, Ordering::SeqCst); }
Err(..) => break,
}
}
});
loop {
extern { fn usleep(usec: usize); }
unsafe { usleep(1000000); }
println!("{}", CNT.swap(0, Ordering::SeqCst));
}
}
$ ./foo
9722724299
9742516277
9742843851
9801236480
9838657536
9723379765
9679798219
$ ./foo
7265430272
7389052928
7271350272
7355170816
7333019648
7377256448
7515996160
7494172672
7448887296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment