title: Using atomics with std::thread in c++11
body: <p>I have a thread that I want to sit in a loop until I'm ready to exit the program, at which point I want it to break out of the loop and exit so I can call <code>std::thread::join</code> on it. In the days of c++03, I would just use a bool protected by a lock in order to tell the thread when to exit. This time I thought I would take advantage of the new atomics library (specifically <code>std::atomic_bool</code>), but I'm having trouble. Below is my test case:</p>
  
  <pre><code>#include &lt;atomic&gt;
  #include &lt;thread&gt;
  #include &lt;cstdio&gt;
  
  using namespace std;
  
  void setBool(atomic_bool&amp; ab)
  {
      ab = true;
  }
  
  int main()
  {
      atomic_bool b;
      b = false;
      thread t(setBool, b);
      t.join();
      printf("Atomic bool value: %d\n", b.load());
      return 0;
  }
  </code></pre>
  
  <p>The declaration of <code>thread t</code> spits out <a href="http://pastebin.com/SpRQW4W1" rel="noreferrer">this</a> monstrosity when I try to compile. The central part of the error seems to be:</p>
  
  <blockquote>
    <p>invalid initialization of non-const reference of type ‘std::atomic_bool&amp;’ from an rvalue of type ‘std::atomic_bool’</p>
  </blockquote>
  
  <p>Why can I not get a reference to an <code>atomic_bool</code>? What should I do instead?</p>