From 10b3b5acf58a0e10e52b11dde307a2641c46376c Mon Sep 17 00:00:00 2001 From: aeon Date: Thu, 11 Jan 2024 15:46:30 +0800 Subject: [PATCH] Add an --no-stdin option to prevent z_sub_thr from reading early EOF --- examples/examples/z_sub_thr.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/examples/z_sub_thr.rs b/examples/examples/z_sub_thr.rs index afdd07ed23..508d31b87b 100644 --- a/examples/examples/z_sub_thr.rs +++ b/examples/examples/z_sub_thr.rs @@ -13,7 +13,7 @@ // use clap::Parser; use std::io::{stdin, Read}; -use std::time::Instant; +use std::time::{Duration, Instant}; use zenoh::config::Config; use zenoh::prelude::sync::*; use zenoh_examples::CommonArgs; @@ -72,7 +72,7 @@ fn main() { // initiate logging env_logger::init(); - let (mut config, m, n) = parse_args(); + let (mut config, m, n, no_stdin) = parse_args(); // A probing procedure for shared memory is performed upon session opening. To enable `z_pub_shm_thr` to operate // over shared memory (and to not fallback on network mode), shared memory needs to be enabled also on the @@ -95,10 +95,16 @@ fn main() { .res() .unwrap(); - for byte in stdin().bytes() { - match byte { - Ok(b'q') => break, - _ => std::thread::yield_now(), + if no_stdin { + loop { + std::thread::park(); + } + } else { + for byte in stdin().bytes() { + match byte { + Ok(b'q') => break, + _ => std::thread::yield_now(), + } } } } @@ -111,11 +117,14 @@ struct Args { #[arg(short, long, default_value = "100000")] /// Number of messages in each throughput measurements. number: usize, + /// Do not read standard input. + #[arg(long)] + no_stdin: bool, #[command(flatten)] common: CommonArgs, } -fn parse_args() -> (Config, usize, usize) { +fn parse_args() -> (Config, usize, usize, bool) { let args = Args::parse(); - (args.common.into(), args.samples, args.number) + (args.common.into(), args.samples, args.number, args.no_stdin) }