简体   繁体   中英

Rust multithread intensive methods which need to access same set of data with Rayon

I use Rayons par_iter() to iterate over different variations of the expensive method I need to run. These runs need to access the same set of checked usizes because they all need to add to it and check it from time to time. I also need them all to shutdown when first thread finishes, this is why I have a kill_switch which will force the iterations to exit when its set to true.

let mut checked: HashSet<usize> = HashSet::new();
let mut kill_switch: bool = false;

permutations.par_iter().for_each(|order| {
    let board = Board::new(board_map.clone(), order.clone());
    let mut bubbles: Vec<(i8, i8)> = Vec::new();
    if let Some(bubbles) = board.solve(&mut bubbles, &mut checked, &kill_switch) {
        kill_switch = true;
        bubbles.into_iter().for_each(|bubble| {
            dbg!(bubble);
        });
    }
})

This is the code I currently have but I get errors for how I'm using checked and kill_switch. How do I make this work?

Errors:

  • cannot borrow checked as mutable, as it is a captured variable in a Fn closure cannot borrow as mutable [E0596]
  • cannot assign to kill_switch , as it is a captured variable in a Fn closure cannot assign [E0594]

To fix the errors, you will need to use RefCells to wrap the checked and kill_switch variables and use the borrow_mut method to get a mutable reference to them in the closure.

Here is an example of how you can modify your code:

use std::cell::RefCell;
use std::collections::HashSet;

let checked: RefCell<HashSet<usize>> = RefCell::new(HashSet::new());
let kill_switch: RefCell<bool> = RefCell::new(false);

permutations.par_iter().for_each(|order| {
    let board = Board::new(board_map.clone(), order.clone());
    let mut bubbles: Vec<(i8, i8)> = Vec::new();
    if let Some(bubbles) = board.solve(&mut bubbles, &mut checked.borrow_mut(), &mut kill_switch.borrow_mut()) {
        *kill_switch.borrow_mut() = true;
        bubbles.into_iter().for_each(|bubble| {
            dbg!(bubble);
        });
    }
})

Note that you will also need to add RefCell as a dependency in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM