在生成的线程中读取不可变值 [英] Reading immutable value inside spawned thread

查看:36
本文介绍了在生成的线程中读取不可变值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让这个例子发挥作用

I'm trying to make this example work

use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc;
use std::thread;
use std::sync::Arc;

struct User {
    reference: String,
    email: String
}

struct UserIndex {
    reference: usize,
    email: usize
}

fn main() {

    let rows = vec![
        vec!["abcd", "test@test.com"],
        vec!["efgh", "test1@test.com"],
        vec!["wfee", "test2@test.com"],
        vec!["rrgr", "test3@test.com"]
    ];

    let user_index = Arc::new(
        UserIndex {
            reference: 0,
            email: 1
        }
    );

    let chunk_len = (rows.len() / 2) as usize;
    let mut chunks = Vec::new();
    for chunk in rows.chunks(chunk_len) {
        chunks.push(chunk.to_owned());
    }

    let (tx, rx): (Sender<Vec<User>>, Receiver<Vec<User>>) = mpsc::channel();

    for chunk in chunks {
        let thread_tx = tx.clone();
        thread::spawn(move || {
            let user_index_cloned = user_index.clone();
            let result = chunk.iter().map( |row|
                User {
                    reference: row[user_index_cloned.reference].to_string(),
                    email: row[user_index_cloned.email].to_string()
                }
            ).collect::<Vec<User>>();
            thread_tx.send(result).unwrap();
        });
    }

    let mut users = Vec::new();
    for _ in 0..chunk_len {
        users.push(rx.recv());
    }

}

但它抛出一个错误

src/main.rs:43:28: 43:38 error: capture of moved value: `user_index` [E0382]
src/main.rs:43          let user_index_cloned = user_index.clone();
                                                ^~~~~~~~~~
note: in expansion of closure expansion
src/main.rs:42:17: 51:4 note: expansion site
note: in expansion of for loop expansion
src/main.rs:40:2: 52:3 note: expansion site
src/main.rs:42:17: 51:4 note: `user_index` moved into closure environment here because it has type `[closure(())]`, which is non-copyable
src/main.rs:42      thread::spawn(move || {
src/main.rs:43          let user_index_cloned = user_index.clone();
src/main.rs:44          let result = chunk.iter().map( |row|
src/main.rs:45              User {
src/main.rs:46                  reference: row[user_index_cloned.reference].to_string(),
src/main.rs:47                  email: row[user_index_cloned.email].to_string()
               ...
note: in expansion of closure expansion
src/main.rs:42:17: 51:4 note: expansion site
note: in expansion of for loop expansion
src/main.rs:40:2: 52:3 note: expansion site
src/main.rs:42:17: 51:4 help: perhaps you meant to use `clone()`?
error: aborting due to previous error

现在根据 这个讨论它应该有效,但是它没有.我在这里错过了什么?

now according to this discussion it should have worked, but it doesn't. What am I missing here?

推荐答案

大功告成.只是Arc 必须在衍生线程的外部被克隆:

You're almost there. It's just that Arc has to be cloned outside of the spawned thread:

for chunk in chunks {
    let thread_tx = tx.clone();
    let user_index_cloned = user_index.clone();
    thread::spawn(move || {
        let result = chunk.iter().map( |row|
            User {
                reference: row[user_index_cloned.reference].to_string(),
                email: row[user_index_cloned.email].to_string()
            }
        ).collect::<Vec<User>>();
        thread_tx.send(result).unwrap();
    });
}

必须这样做,否则 user_index 确实会被移动到线程中,并且在循环中这样做是违反所有权规则的.

This has to be done because otherwise user_index indeed will be moved into the thread, and doing this in a loop is a violation of ownership rules.

这篇关于在生成的线程中读取不可变值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆