#Passing data to a thread

8 messages · Page 1 of 1 (latest)

tight cairn
#

Hi
I'm trying to pass a reference to a Vec<Vec<u8>> into a thread from a struct, but I can't get that to work. I've tried the clone trait, referencing to the array, creating a new variable and referencing to that instead, cloning the whole array. But I keep getting compiler errors.

borrowed data escapes outside of associated function
`self` escapes the associated function body here

thread::spawn(move || {
                ^^^^^^^
                |
                value moved into closure here, in previous iteration of loop
                value used here after move
pub fn step_multit(&mut self)
            {
                let mut th: Vec<JoinHandle<()>> = Vec::with_capacity((self.rows * self.blocks) as usize);
                for r in 0..self.rows {
                    for b in 0..self.blocks {
                        
                        th.push(
                            thread::spawn(move || {
                                let r = r.clone();
                                let b = b.clone();// These ones copied over fine

                                // ...

                            })
                        );
                    }
                }
                for t in th {
                    t.join().unwrap();
                }
            }
        }
    }
signal cedar
#

thread::spawn can't take borrowing references, because it can't guarantee that the child thread won't outlive the parent. But thread::scope should work here

frozen abyss
#

or you can clone before the thread into a new variable

#

and move that variable

crimson breach
tight cairn
#

Ok. I think I got it working now, I get the same result like when I run everything in a single thread. Criterion is package for running benchmarks right?