In my project I need to do something like:
use std::thread;
use std::time::Duration;
struct A {
pub ints: Vec<u8>,
}
impl A {
fn new() -> A {
let mut a = A {
ints: vec![1, 5, 6, 2, 3, 4],
};
a.timer();
a
}
fn timer(&mut self) {
thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(1));
self.ints.remove(0);
}
});
}
}
fn main() {
let a = A::new();
loop {
println!("Remaining elements: {:?}", a.ints);
}
}
The idea is that some struct contains a vector of elements. These elements should be removed from the vector after some period of time. Think of it as a periodic timer that checks something and performs an action on mutable object (removes an element). This thread also needs to be dropped if the object on which it is working on is deleted. So I guess it should be a member of a struct of which it is manipulating.
The problem with the code above is that it has a lot of borrow errors and I don't understand how to do that.
I have seen few questions like this but each of them was about manipulating scalar in a thread. The reason why I can't apply it here is because the thread should be something that is inside A
struct and it should call remove
on a vector that is a member of that struct.
I guess I should use Arc
or something like that but don't really understand how to use it here.
Indeed that is the simplest solution:
You can wrap the
ints
field in anArc
, but then you wouldn't be able to modify theVec
, so you also wrap it in aMutex
:Then you can clone the
Arc
to receive a second handle to the same memory.Instead of directly accessing the
Vec
, you then need tolock
theMutex
, which can fail if another thread panicked while accessing theMutex
.