title: How do I share access to an AtomicBool between threads?
body: <p>I have this little program - essentially I want one thread to be able to tell the other to stop via a shared boolean in a struct.  </p>
  
  <pre><code>use std::thread;
  use std::thread::JoinHandle;
  use std::time::Duration;
  use std::sync::atomic::{AtomicBool, Ordering};
  
  struct Test {
      should_stop: AtomicBool,
      running_thread_handles: Vec&lt;JoinHandle&lt;()&gt;&gt;
  }
  
  impl Test {
      fn new() -&gt; Test {
          Test { 
              should_stop: AtomicBool::new(false), 
              running_thread_handles: Vec::new() 
          }
      }
  
      fn stop(&amp;mut self) {
          self.should_stop.store(true, Ordering::Relaxed);
      }
  
      fn start(&amp;mut self) {
          let handle = thread::spawn(move || {
              loop {
                  println!("Looping");
                  thread::sleep(Duration::from_millis(500));
  
                  // I want to effectively do this...
                  /*
                  if self.stop_bool.load(Ordering::Relaxed) {
                      println!("Stopping");
                      break;
                  }*/
              }
          });
  
          self.running_thread_handles.push(handle);
      }
  }
  
  impl Drop for Test {
      fn drop(&amp;mut self) {
          self.stop();
  
          // Here I want to iterate the self.running_thread_handles and
          // make sure they are cleaned up
      }
  }
  
  // I expect to see a 4 "Looping" messages and then a "Stopping" message
  fn main() {
     let mut test = Test::new();
     test.start();
     thread::sleep(Duration::from_millis(2000));
     test.stop();
  }
  </code></pre>
  
  <p>Perhaps there is a better approach altogether but I figure this is probably a good way to understand the lifetime stuff a bit.</p>
  
  <p>I thought that all I need is an <code>Arc</code> so I tried this:</p>
  
  <pre><code>fn start(&amp;mut self) {
      let stop_bool = Arc::new(&amp;self.should_stop).clone();
  
      let handle = thread::spawn(move || {
          loop {
              println!("Looping");
              thread::sleep(Duration::from_millis(500));
  
              if stop_bool.load(Ordering::Relaxed) {
                  println!("Stopping");
                  break;
              }
          }
      });
  
      self.running_thread_handles.push(handle);
  }
  </code></pre>
  
  <p>That gives me this error:</p>
  
  <blockquote>
    <p>error: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements</p>
  </blockquote>
  
  <p>I figure the compiler does not understand the lifetime of the threads since all I am doing is storing the handles in a vector so I need to tell it somehow, but how?  </p>
  
  <p>If I declare the struct like this would I be getting nearer to making it work?</p>
  
  <pre><code>struct Test&lt;'a&gt; {
      should_stop: AtomicBool,
      running_thread_handles: &amp;'a Vec&lt;JoinHandle&lt;()&gt;&gt;
  }
  </code></pre>
  
  <p>I also have secondary, related, troubles that I cannot for the life of me figure out how to iterate through my vector of handles and call any functions on them in the <code>Drop</code> impl.  I suppose that the solution to that would be related, so I will not ask that.</p>