【发布时间】:2017-07-27 15:18:18
【问题描述】:
我正在尝试使用 Rayon 的 par_iter() 优化我的功能。
单线程版本类似于:
fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
let result = txs.iter().map(|tx| {
tx.verify_and_store(store)
}).collect();
...
}
每个Store实例只能被一个线程使用,但是Store的多个实例可以同时使用,所以我可以通过clone-ing store来实现这个多线程:
fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
let result = txs.par_iter().map(|tx| {
let mut local_store = store.clone();
tx.verify_and_store(&mut local_store)
}).collect();
...
}
但是,这会在每次迭代中克隆store,这太慢了。我想每个线程使用一个商店实例。
人造丝可以做到这一点吗?还是我应该求助于手动线程和工作队列?
【问题讨论】:
标签: multithreading rust rayon