Reputation: 1545
I have a function:
fn awesome_function<F>(v: Vec<u64>, f: F)
where F: Fn(u64) -> String
{ /* ... */ }
Is there any way to call that passed in function inside a spawned thread? Something like:
...
thread::spawn(move || f(1));
...
I've tried a few different ways and I get an error error: the trait core::marker::Send is not implemented for the type F [E0277]
Upvotes: 2
Views: 199
Reputation: 430981
Absolutely. The main thing you have to do is restrict F
to also be Send
:
use std::thread;
fn awesome_function<F>(v: Vec<u64>, f: F) -> String
where F: Fn(u64) + Send + 'static
{
thread::spawn(move || f(1));
unimplemented!()
}
fn main() {}
You also need to restrict it to 'static
, as also required by thread::spawn
.
Upvotes: 5