将向量复制到线程

时间:2015-05-31 22:48:59

标签: multithreading concurrency rust

考虑以下代码,我希望能够在我的帖子中访问clientrequests,目前我没有:

for _x in 0..100 {
        let handle = thread::spawn(move || {
            let start = time::precise_time_s();

            let res = client.get("http://jacob.uk.com")
                .header(Connection::close()) 
                .send().unwrap();

            let end = time::precise_time_s();

            requests.push(Request::new(end-start));
        });

        handle.join().unwrap()
    }

我收到以下编译错误:

   Compiling Herd v0.1.0 (file:///Users/jacobclark/Desktop/LearningRust/Herd)
src/main.rs:41:23: 41:29 error: capture of moved value: `client`
src/main.rs:41             let res = client.get("http://jacob.uk.com")
                                     ^~~~~~
src/main.rs:38:41: 48:10 note: `client` moved into closure environment here because it has type `[closure(())]`, which is non-copyable
src/main.rs:38         let handle = thread::spawn(move || {
src/main.rs:39             let start = time::precise_time_s();
src/main.rs:40         
src/main.rs:41             let res = client.get("http://jacob.uk.com")
src/main.rs:42                 .header(Connection::close()) 
src/main.rs:43                 .send().unwrap();
               ...
src/main.rs:38:41: 48:10 help: perhaps you meant to use `clone()`?
src/main.rs:47:13: 47:21 error: capture of moved value: `requests`
src/main.rs:47             requests.push(Request::new(end-start));
                           ^~~~~~~~
src/main.rs:38:41: 48:10 note: `requests` moved into closure environment here because it has type `[closure(())]`, which is non-copyable
src/main.rs:38         let handle = thread::spawn(move || {
src/main.rs:39             let start = time::precise_time_s();
src/main.rs:40         
src/main.rs:41             let res = client.get("http://jacob.uk.com")
src/main.rs:42                 .header(Connection::close()) 
src/main.rs:43                 .send().unwrap();
               ...
src/main.rs:38:41: 48:10 help: perhaps you meant to use `clone()`?
src/main.rs:53:24: 53:32 error: use of moved value: `requests`
src/main.rs:53     Request::mean_time(requests);
                                      ^~~~~~~~
src/main.rs:38:41: 48:10 note: `requests` moved into closure environment here because it has type `[closure(())]`, which is non-copyable
src/main.rs:38         let handle = thread::spawn(move || {
src/main.rs:39             let start = time::precise_time_s();
src/main.rs:40         
src/main.rs:41             let res = client.get("http://jacob.uk.com")
src/main.rs:42                 .header(Connection::close()) 
src/main.rs:43                 .send().unwrap();
               ...
src/main.rs:38:41: 48:10 help: perhaps you meant to use `clone()`?
error: aborting due to 3 previous errors
Could not compile `Herd`

1 个答案:

答案 0 :(得分:5)

首先,最少的例子非常有用:

use std::thread;

fn main() {
    let mut items = Vec::new();

    for _ in 0..100 {
        thread::spawn(move || {
            items.push(());
        });
    }
}

那么这里的问题是什么?好吧,你将items移动到关闭100次 - 但你只能移动一次!

要跨多个线程共享数据,您需要

  • 要删除数据竞赛 - 将其放入Mutex(或不要改变它)。

  • 为了让它保持活力 - 将其放入Arc A tomically R eference- C 指针)。

use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let items = Arc::new(Mutex::new(Vec::new()));

    for _ in 0..10 {
        let thread_items = items.clone();

        let handle = thread::spawn(move || {
            thread_items.lock().unwrap().push(());
        });

        handle.join().unwrap();
    }

    println!("{:?}", items);
}