1use std::{env, sync::{atomic::{AtomicU64, Ordering}, Arc}, thread::{self, JoinHandle}};
2
3use libmapper_rs::graph::Graph;
4
5
6fn main() {
7 let graph = Arc::new(Graph::create());
8 let counter: Arc<AtomicU64> = Arc::new(AtomicU64::new(0));
9
10 let mut threads = Vec::<JoinHandle<()>>::new();
11
12 {
13 let graph = Arc::clone(&graph);
15 let thread = std::thread::spawn(move || {
16 loop {
17 graph.poll();
18 }
19 });
20 threads.push(thread);
21 }
22
23 let mut num_workers = 10;
24 let arg = env::args().skip(1).next();
25 if arg.is_some() {
26 num_workers = arg.unwrap().parse::<i32>().unwrap_or(10);
27 }
28
29 for _ in 0..num_workers {
31 let graph = graph.clone();
32 let id_counter = counter.clone();
33 let thread = std::thread::spawn(move || {
34 let name = format!("rust_{:?}", thread::current().id());
35 loop {
36 let _dev = libmapper_rs::device::Device::create_from_graph(&name, &graph);
37 loop {
38 _dev.poll();
39 if _dev.is_ready() {
40 break;
41 }
42 }
43 let signal = _dev.create_signal::<f32>("test_sig", libmapper_rs::constants::mpr_dir::MPR_DIR_OUT);
44 thread::sleep(std::time::Duration::from_millis(id_counter.fetch_add(1, Ordering::SeqCst) as u64));
45 drop(signal);
46 drop(_dev);
47 }
48 });
49 threads.push(thread);
50 }
51
52 for thread in threads {
53 thread.join().unwrap();
54 }
55}