I have the following async task:
let forever = task::spawn(async {
let mut interval = time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
report_generator::task();
}
});
The 'task()' function needs to receive as parameter a reference to a data structure ( an HashMap ) because it needs to write it into a file at regular interval.
The problem is that, the HashMap is used by other threads which are used to fill it so it is used inside a standard Mutex.
So, if i try to write the following code:
let forever = task::spawn(async {
let mut interval = time::interval(Duration::from_secs(5));
let mut guard = self.m.lock().unwrap();
loop {
interval.tick().await;
report_generator::task(&guard.map).await;
}
});
clearly i get this error : future cannot be sent between threads safely.
How can solve this issue?
Should I implement the Send trait or is there a faster and smarter way?