I am trying to fire up a new thread using some heap data in Rust and I am getting a bunch of errors that stem from the need of the data to have \'static
lifetim
The error about 'static
is because the new thread created within thread::spawn
may outlive the invocation of threaded_search
during which the thread is initially created, which means the thread must not be permitted to use any local variables from threaded_search
with a lifetime shorter than 'static
.
In your code the new thread is referring to strings_as_slice1
and td_arc
.
Generally with thread::spawn
and Arc
you will want to move ownership of one reference count into the thread and have the thread access whatever it needs through that reference counted pointer rather than from the enclosing short-lived scope directly.
fn threaded_search(td_arc: &Arc<ThreadData>) {
// Increment reference count that we can move into the new thread.
let td_arc = td_arc.clone();
thread::spawn(move || {
perform_search(&td_arc.vector_of_strings[0..td_arc.quotient], &td_arc.terms);
});
}