Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there another option to share an Arc in multiple closures besides cloning it before each closure?

Tags:

closures

rust

I have something like this:

use std::sync::Arc;

fn main() {
    let arc = Arc::new(42);
    move || { arc.clone() };
    move || { arc.clone() };
}

I am getting:

error[E0382]: capture of moved value: `arc`
 --> src/main.rs:6:19
  |
5 |         move || { arc.clone() };
  |         ------- value moved (into closure) here
6 |         move || { arc.clone() };
  |                   ^^^ value captured here after move
  |
  = note: move occurs because `arc` has type `std::sync::Arc<i32>`, which does not implement the `Copy` trait

I understand why I am getting this: the clone isn't called before arc is passed to the closure. I can fix this by defining each closure in a function and clone the Arc before passing it to the closure, but is there another option?

like image 470
Tomer Avatar asked Jul 11 '15 17:07

Tomer


People also ask

Is Arc thread safe Rust?

Thread Safety Unlike Rc<T> , Arc<T> uses atomic operations for its reference counting. This means that it is thread-safe.

What is arch length rust?

An integer the size of which is arch will be 32 bits on an x86 machine and 64 bits on an x64 machine.


1 Answers

There is no way around it. You should clone the Arc before it is used in a closure. The common pattern is to re-bind the cloned Arc to the same name in a nested scope:

use std::sync::Arc;

fn main() {    
    let arc = Arc::new(42);
    {
        let arc = arc.clone();
        move || { /* do something with arc */ };
    }
    {
        let arc = arc.clone();
        move || { /* do something else with arc */ };
    }
}

This is usually done together with thread::spawn():

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

const NUM_THREADS: usize = 4;

fn main() {
    let arc = Arc::new(Mutex::new(42));
    for _ in 0..NUM_THREADS {
        let arc = arc.clone();
        thread::spawn(move || {
            let mut shared_data = arc.lock().unwrap();
            *shared_data += 1;
        });
    }
}
like image 57
Vladimir Matveev Avatar answered Oct 01 '22 14:10

Vladimir Matveev