title: EXC_BAD_ACCESS error? Boost Multithreading/Mutex Exception
body: <p>I seem to be having difficulty understanding what is wrong with my code below. The program runs correctly, but at the line</p>
  
  <pre><code>startCounter.join();
  </code></pre>
  
  <p>My program seems to crash, with a error code EXC_BAD_ACCESS (code=13, address=0x0). I am using XCode.</p>
  
  <p>Everything up until that line works correctly; the program prints out the value of n each second as another thread increments the value indefinitely. What am I doing wrong?</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;boost/thread.hpp&gt;
  
  boost::mutex mtx;
  
  class Worker
  {
  public:
      Worker(int n);
      void increment();
      void printValue();
  private:
      int n;
  };
  
  int main()
  {
      Worker test(5);
  
      boost::thread startCounter(boost::bind(&amp;Worker::increment, &amp;test));
      std::cout &lt;&lt; "Increment Thread Started..." &lt;&lt; std::endl;
  
      boost::thread startPrinter(boost::bind(&amp;Worker::printValue, &amp;test));
      std::cout &lt;&lt; "Print Thread Started..." &lt;&lt; std::endl;
  
      startPrinter.join();
      startCounter.join();
  
      std::cout &lt;&lt; "Program Complete." &lt;&lt; std::endl;
  
      return EXIT_SUCCESS;
  }
  
  Worker::Worker(int n) : n(n)
  {
      std::cout &lt;&lt; "Initial Value: " &lt;&lt; n &lt;&lt; std::endl;
  }
  
  void Worker::increment()
  {
      for (int i = 0; i &lt; 60000000; i++) {
          boost::lock_guard&lt;boost::mutex&gt; lock(mtx);
          n = i;
      }
  
      std::cout &lt;&lt; "Increment Thread Finished. Final Value: " &lt;&lt; n &lt;&lt; "." &lt;&lt; std::endl;
  }
  
  void Worker::printValue()
  {
      for (int i = 0; i &lt; 10; i++) {
          {
              boost::lock_guard&lt;boost::mutex&gt; lock(mtx);
              std::cout &lt;&lt; "Current value: " &lt;&lt; n &lt;&lt; std::endl;
          }
          boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
      }
  
      std::cout &lt;&lt; "Print Thread Finished." &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Thanks a lot!</p>