Updateing stuct bool from inside thread

Hi everyone just started using Rust. I am trying to have a scanning bool be updated when a thread is finished without waiting for it to finish so my idea was to try and have the thread call and update_scanning on self but I have come across an issue maybe using Arc could help?

`self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
this data with an anonymous lifetime `'_`...

I am not really understanding this issue

use crate::db::get_connection;
use anyhow::Result;
use std::sync::{atomic::AtomicBool, Arc};

pub mod scanner;
pub mod tag_helper;
#[derive(Clone)]
pub struct Scanner {
    scanning: AtomicBool,
}
impl Scanner {
    pub fn new() -> Result<Scanner> {
        let scanner = Scanner {
            scanning: AtomicBool::new(false),
        };
        Ok(scanner)
    }

    fn update_scanning(&mut self, status: AtomicBool) {
        self.scanning = status
    }
    pub fn get_status(&self) -> AtomicBool {
        return self.scanning;
    }
    pub async fn start_scan(&mut self) {
        tokio::spawn(async move {
            let db_connection = get_connection().await.expect("Failed to connect to db");
            use std::time::Instant;
            let before = Instant::now();
            scanner::walk(&db_connection).await.unwrap();
            tracing::info!("Scan completed in: {:.2?}", before.elapsed());
            self.update_scanning(AtomicBool::new(false))
            //val = Arc::new(false);
        });
    }
}

You can't access borrowed data from a thread, because a thread might live for an arbitrarily long time. If you want to use Arc for holding on to some pointed data, then you have to own a copy of the Arc. Thus, you can't do this from a method that takes &mut self (or &self); if you need to access the field through a reference to the Scanner struct itself, then you have to put the whole Scanner behind an Arc.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.