产生线程并获得未来结果的最佳方法是什么?

问题描述 投票:0回答:1

是否有更好的方法来执行以下操作?

use futures::channel::oneshot; // 0.3.4
use std::thread;

pub async fn spawn<Y>(f: impl Fn() -> Y + Send + Sync + 'static) -> Y
where
    Y: Send + 'static,
{
    let (sender, receiver) = oneshot::channel::<Y>();
    thread::spawn(move || sender.send(f()));
    receiver.await.unwrap()
}
multithreading rust future
1个回答
0
投票

tokio::task::spawn_blocking是我想要的。

© www.soinside.com 2019 - 2024. All rights reserved.