Last active
February 25, 2016 21:40
-
-
Save ashish01/e0e489e4849412e21a57 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(time2)] | |
use std::time::Instant; | |
use std::thread; | |
use std::sync::mpsc; | |
use std::io; | |
struct ScopedPerformanceBlock<'a> { | |
sender: &'a mpsc::Sender<u32>, | |
creation_time: Instant, | |
} | |
impl<'a> Drop for ScopedPerformanceBlock<'a> { | |
fn drop(&mut self) { | |
let diff = self.creation_time.elapsed().subsec_nanos(); | |
self.sender.send(diff).unwrap(); | |
} | |
} | |
impl<'a> ScopedPerformanceBlock<'a> { | |
fn new(sender: &mpsc::Sender<u32>) -> ScopedPerformanceBlock { | |
ScopedPerformanceBlock { | |
creation_time: Instant::now(), | |
sender: sender | |
} | |
} | |
} | |
fn main() { | |
let (tx, rx) = mpsc::channel::<u32>(); | |
thread::spawn(move || { | |
loop { | |
println!("{:?}", rx.recv().unwrap()); | |
} | |
}); | |
for i in 1..100 | |
{ | |
let x = ScopedPerformanceBlock::new(&tx); | |
} | |
let mut input = String::new(); | |
io::stdin().read_line(&mut input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment