parallel_recv/
parallel-recv.rs1use std;
6use unbounded_spsc;
7
8const MESSAGE_COUNT : u64 = 10_000_000;
9
10static SENDER_STARTED : std::sync::atomic::AtomicBool =
11 std::sync::atomic::AtomicBool::new (false);
12static RECEIVER_STARTED : std::sync::atomic::AtomicBool =
13 std::sync::atomic::AtomicBool::new (false);
14
15#[derive(Debug,PartialEq)]
16struct Mystruct {
17 x : f64,
18 y : f64,
19 z : f64
20}
21
22fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
23 let mut counter = 0;
24 SENDER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
25 while !RECEIVER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
27 std::hint::spin_loop()
28 }
29 let start_time = std::time::SystemTime::now();
30 while counter < MESSAGE_COUNT {
31 sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
32 counter += 1;
33 }
34 let duration = start_time.elapsed().unwrap();
35 let duration_ns
36 = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
37 println!("sendfun duration ns: {duration_ns}");
38 println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
39}
40
41fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
42 RECEIVER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
43 while !SENDER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
45 std::hint::spin_loop()
46 }
47 let start_time = std::time::SystemTime::now();
48 while let Ok (_m) = receiver.recv() { }
49 let duration = start_time.elapsed().unwrap();
50 let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
51 as u64;
52 println!("recvfun duration ns: {duration_ns}");
53 println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
54 println!("buffer ending capacity: {}", receiver.capacity());
55}
56
57fn main() {
58 println!("main...");
59 let (sender, receiver) = unbounded_spsc::channel();
60 let join_sender = std::thread::spawn (move || sendfun (sender));
61 let join_receiver = std::thread::spawn (move || recvfun (receiver));
62 join_sender.join().unwrap();
63 join_receiver.join().unwrap();
64 println!("...main");
65}
66