-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add a multithread example that updates a SensorData cache
- Loading branch information
1 parent
a999413
commit c58c0f3
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use navigator_rs::Navigator; | ||
use std::sync::{Arc, Mutex, RwLock}; | ||
use std::thread::{self, sleep}; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
println!("Creating your SensorData cache!"); | ||
let sensor_data = Arc::new(RwLock::new(navigator_rs::SensorData::default())); | ||
|
||
println!("Creating your navigator module!"); | ||
let nav = Arc::new(Mutex::new(Navigator::new())); | ||
|
||
println!("Setting up your navigator, ahoy!"); | ||
nav.lock().unwrap().init(); | ||
|
||
// This code block creates a thread that updates the `sensor_data` periodically. | ||
let sensor_data_cloned = sensor_data.clone(); | ||
let nav_cloned = nav.clone(); | ||
thread::Builder::new() | ||
.name("Sensor reader".into()) | ||
.spawn(move || loop { | ||
if let Ok(mut sensor_data) = sensor_data_cloned.write() { | ||
*sensor_data = nav_cloned.lock().unwrap().read_all(); | ||
println!("Updated value: {sensor_data:?}"); | ||
} | ||
sleep(Duration::from_millis(10000)); | ||
}) | ||
.expect("Failed to spawn the sensor reader thread"); | ||
|
||
// This code block could be also other thread, but just run on the main one. | ||
// This could work as a server get handler, or an instance that can be used by different services to monitor and analysis. | ||
loop { | ||
if let Ok(sensor_data) = sensor_data.read() { | ||
println!("Read SensorData: {sensor_data:?}"); | ||
} | ||
sleep(Duration::from_millis(1000)); | ||
} | ||
} |