source: benchmark/schedint/rust.rs@ 044ae62

ADT
Last change on this file since 044ae62 was 26fd986, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update benchmarks for concurrency paper

  • Property mode set to 100644
File size: 982 bytes
Line 
1use std::env;
2use std::process;
3use std::thread;
4use std::sync::{Arc, Mutex, Condvar};
5use std::time::Instant;
6
7fn main() {
8 let mut times : u32 = 500000;
9 let args: Vec<String> = env::args().collect();
10 if args.len() > 2 { process::exit( 1 ); }
11 if args.len() == 2 { times = args[1].parse().unwrap(); }
12
13 let m = Arc::new(Mutex::new(0));
14 let c = Arc::new(Condvar::new());
15
16 let m2 = Arc::clone(&m);
17 let c2 = Arc::clone(&c);
18
19 let th = thread::spawn( move || {
20 while *m2.lock().unwrap() == 0 { // waiter must start first
21 thread::yield_now();
22 }
23 let start = Instant::now();
24 while *m2.lock().unwrap() == 1 {
25 c2.notify_one();
26 }
27 let duration = start.elapsed() / times;
28 println!( "{:?}", duration.as_nanos() );
29 });
30 {
31 let mut sc = m.lock().unwrap();
32 *sc = 1;
33 for _ in 1..times {
34 sc = c.wait(sc).unwrap();
35 }
36 *sc = 0;
37 }
38 th.join().unwrap();
39}
40
41// Local Variables: //
42// tab-width: 4 //
43// compile-command: "rustc -C opt-level=3 rust.rs" //
44// End: //
45
Note: See TracBrowser for help on using the repository browser.