Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Closure might outlive current function even though it is joined

fn main() {
    let foo = 5;

    std::thread::spawn(|| { // closure may outlive the current function, but it borrows `foo`, which is owned by the current function
        println!("{}", foo);
    })
    .join()
    .unwrap();
}

Moving the value is not an option since it have to make multiple threads

The situation in my code is a bit more complicated, but I still need threads and I ended up moving and Arc into it instead of just a reference

Here is a link to the line in the project, but you don’t have to read it: https://github.com/Antosser/web-crawler/blob/5d23ffa7ed64c772080c7be08a26bda575028c7c/src/main.rs#L291

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The compiler does not know it is joined. It does not apply any special analysis to see if threads are joined.

However, if you join your threads, you can use scoped threads to access variables:

fn main() {
    let foo = 5;

    std::thread::scope(|s| {
        s.spawn(|| {
            println!("{}", foo);
        });
        // Thread implicitly joined here.
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading