I'm working on a task scheduling framework in Rust that uses work stealing between executors. Each executor has it's own thread which I have spawned using code that looks like this:
let t_handle = thread::spawn(move || {...}
Abstractly, there must be a way for this thread to be able to see the work queues of other threads and perform a stealing operation on that queue.
The way I'm doing this right now is by passing a function (Fn()
) into the struct's new
method called on_steal
which closes over a borrow of vec<Stealer>
.
Rust yells at me in two places:
- First it wants a lifetime parameter for
on_steal
, and specifically it's asking forstatic
. - After blessing that
on_steal
with'static
, the lifetime for my borrow ofvec<Stealer>
becomes an issue.
The solution I have currently is to wrap my vec<Stealer>
in a Arc<mutex<_>>
, which is fine. But I'm wondering if there is a more elegant solution for specifying lifetime params of threads in Rust.
I'm looking to express some kind of guarantee that one thread will outlive another, thus allowing me to lend out references from the more longevous thread to the shorter lived thread without requiring those refs to be static.