Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.10 KB | None | 0 0
  1. extern crate time;
  2.  
  3. use std::fs::{File};
  4. use std::io::Write;
  5.  
  6. use std::thread;
  7. use std::time::Duration;
  8.  
  9. const SYNCER: bool = true;
  10.  
  11. fn worker(mut f: File) {
  12.     let data = [0xffu8; 65536];
  13.     for _ in 0..16384 {
  14.         f.write_all(&data).unwrap();
  15.         thread::sleep(Duration::new(0, 100_000));
  16.     }
  17. }
  18.  
  19. fn syncer(f: File) {
  20.     loop {
  21.         thread::sleep(Duration::from_millis(1));
  22.         if SYNCER {
  23.             f.sync_all().unwrap();
  24.         }
  25.     }
  26. }
  27.  
  28. fn main() {
  29.     println!("{}", if SYNCER { "sync" } else { "no sync" });
  30.  
  31.     let tm0 = time::precise_time_s();
  32.     let f = File::create("output").unwrap();
  33.     thread::spawn({
  34.         let f = f.try_clone().unwrap();
  35.         move || syncer(f)
  36.     });
  37.     let worker = thread::spawn({
  38.         let f = f.try_clone().unwrap();
  39.         move || worker(f)
  40.     });
  41.     worker.join().unwrap();
  42.     let tm1 = time::precise_time_s();
  43.     println!("writing: {}", tm1 - tm0);
  44.     f.sync_all().unwrap();
  45.     let tm2 = time::precise_time_s();
  46.     println!("syncing: {}", tm2 - tm1);
  47.     println!("total: {}", tm2 - tm0);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement