|
1 | 1 | use std::thread; |
2 | 2 |
|
3 | | -use clap::{App, Arg}; |
| 3 | +use clap::Parser; |
4 | 4 | use shared_memory::*; |
5 | 5 |
|
| 6 | +/// Spawns N threads that increment a value to 100 |
| 7 | +#[derive(Parser)] |
| 8 | +#[clap(author, version, about)] |
| 9 | +struct Args { |
| 10 | + /// Number of threads to spawn |
| 11 | + num_threads: usize, |
| 12 | + |
| 13 | + /// Count to this value |
| 14 | + #[clap(long, short, default_value_t = 50)] |
| 15 | + count_to: u8, |
| 16 | +} |
| 17 | + |
6 | 18 | fn main() { |
7 | 19 | env_logger::init(); |
| 20 | + let args = Args::parse(); |
8 | 21 |
|
9 | | - // Get number of thread argument |
10 | | - let matches = App::new("Basic Example") |
11 | | - .about("Spawns N threads that increment a value to 100") |
12 | | - .arg( |
13 | | - Arg::with_name("num_threads") |
14 | | - .help("Number of threads to spawn") |
15 | | - .required(true) |
16 | | - .takes_value(true), |
17 | | - ) |
18 | | - .arg( |
19 | | - Arg::with_name("count_to") |
20 | | - .help("Count to this value") |
21 | | - .short("c") |
22 | | - .long("count") |
23 | | - .default_value("50") |
24 | | - .takes_value(true) |
25 | | - ) |
26 | | - .get_matches(); |
27 | | - let num_threads: usize = matches |
28 | | - .value_of("num_threads") |
29 | | - .unwrap() |
30 | | - .parse() |
31 | | - .expect("Invalid number passed for num_threads"); |
32 | | - |
33 | | - let max: u8 = matches |
34 | | - .value_of("count_to") |
35 | | - .unwrap() |
36 | | - .parse() |
37 | | - .expect("Invalid number passed for count_to"); |
38 | | - |
39 | | - if num_threads < 1 { |
| 22 | + if args.num_threads < 1 { |
40 | 23 | eprintln!("Invalid number of threads"); |
41 | 24 | return; |
42 | 25 | } |
43 | 26 |
|
44 | | - let mut threads = Vec::with_capacity(num_threads); |
| 27 | + let mut threads = Vec::with_capacity(args.num_threads); |
45 | 28 | let _ = std::fs::remove_file("basic_mapping"); |
46 | | - |
| 29 | + let max = args.count_to; |
47 | 30 | // Spawn N threads |
48 | | - for i in 0..num_threads { |
| 31 | + for i in 0..args.num_threads { |
49 | 32 | let thread_id = i + 1; |
50 | 33 | threads.push(thread::spawn(move || { |
51 | 34 | increment_value("basic_mapping", thread_id, max); |
|
0 commit comments