forked from elast0ny/shared_memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.rs
75 lines (63 loc) · 1.96 KB
/
basic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::thread;
use clap::Parser;
use shared_memory::*;
/// Spawns N threads that increment a value to 100
#[derive(Parser)]
#[clap(author, version, about)]
struct Args {
/// Number of threads to spawn
num_threads: usize,
/// Count to this value
#[clap(long, short, default_value_t = 50)]
count_to: u8,
}
fn main() {
env_logger::init();
let args = Args::parse();
if args.num_threads < 1 {
eprintln!("Invalid number of threads");
return;
}
let mut threads = Vec::with_capacity(args.num_threads);
let _ = std::fs::remove_file("basic_mapping");
let max = args.count_to;
// Spawn N threads
for i in 0..args.num_threads {
let thread_id = i + 1;
threads.push(thread::spawn(move || {
increment_value("basic_mapping", thread_id, max);
}));
}
// Wait for threads to exit
for t in threads.drain(..) {
t.join().unwrap();
}
}
/// Increments a value that lives in shared memory
fn increment_value(shmem_flink: &str, thread_num: usize, max: u8) {
// Create or open the shared memory mapping
let shmem = match ShmemConf::new().size(4096).flink(shmem_flink).create() {
Ok(m) => m,
Err(ShmemError::LinkExists) => ShmemConf::new().flink(shmem_flink).open().unwrap(),
Err(e) => {
eprintln!("Unable to create or open shmem flink {shmem_flink} : {e}");
return;
}
};
// Get pointer to the shared memory
let raw_ptr = shmem.as_ptr();
// WARNING: This is prone to race conditions as no sync/locking is used
unsafe {
while std::ptr::read_volatile(raw_ptr) < max {
// Increment shared value by one
*raw_ptr += 1;
println!(
"[thread:{}] {}",
thread_num,
std::ptr::read_volatile(raw_ptr)
);
// Sleep for a bit
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
}