0

I'm trying to adapt code from this gist to make something like a pool of workers that could update data simultaneously for me, so I could get it from them via mpsc::channel.

I want to build this pool of workers using a Vec<String> as input, telling it how many workers will be created. I guess the input must be a Vec<String> because it is dynamically built before the pool is created.

This is a basic snippet for result replication:

use std::{
    collections::HashMap,
    sync::{
        mpsc::{self, Receiver, Sender},
        Arc, Mutex, RwLock,
    },
    thread,
};

#[derive(Debug)]
pub struct Pool {
    workers: RwLock<HashMap<String, Arc<Mutex<Worker>>>>,
}

impl Pool {
    pub fn new(symbols: Vec<String>) -> Pool {
        let mut workers: HashMap<String, Arc<Mutex<Worker>>> = HashMap::new();
        for (s, w) in symbols.iter().map(|symbol| {
            (
                symbol,
                Arc::new(Mutex::new({
                    let (in_tx, in_rx): (Sender<i32>, Receiver<i32>) = mpsc::channel();
                    let (out_tx, out_rx) = mpsc::channel();
                    let worker = Worker {
                        in_tx: in_tx,
                        out_rx: out_rx,
                    };
                    thread::spawn(move || {
                        println!("{}", symbol);
                    });
                    worker
                })),
            )
        }) {
            workers.insert(s.to_string(), w);
        }
        Pool {
            workers: RwLock::new(workers),
        }
    }
}

#[derive(Debug)]
struct Worker {
    pub in_tx: Sender<i32>,
    pub out_rx: Receiver<i32>,
}

The compiler keeps complaining:

error[E0597]: `symbols` does not live long enough
  --> src/lib.rs:18:23
   |
18 |         for (s, w) in symbols.iter().map(|symbol| {
   |                       ^^^^^^^-------
   |                       |
   |                       borrowed value does not live long enough
   |                       argument requires that `symbols` is borrowed for `'static`
...
40 |     }
   |     - `symbols` dropped here while still borrowed

I think I kind of understand the reasons why it's happening, but I can't figure a way of working around this.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Gabriel Milan
  • 702
  • 2
  • 7
  • 21
  • 1
    It looks like your question might be answered by the answers of [What is the difference between iter and into_iter?](https://stackoverflow.com/q/34733811/155423); [How can I pass a reference to a stack variable to a thread?](https://stackoverflow.com/q/32750829/155423); [Is there another option to share an Arc in multiple closures besides cloning it before each closure?](https://stackoverflow.com/q/31360003/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 09 '20 at 18:22
  • 1
    [The duplicates applied](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f3d7cbfc6db65b0c224a0e14596048f3). TL;DR — you need to pass an owned `String` into the spawned thread. – Shepmaster Dec 09 '20 at 18:24
  • 1
    Awesome! Thank you very much. – Gabriel Milan Dec 09 '20 at 18:33

0 Answers0