title: Using boost::lock_guard for simple shared data locking
body: <p>I am a newcomer to the Boost library, and am trying to implement a simple producer and consumer threads that operate on a shared queue. My example implementation looks like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;deque&gt;
  #include &lt;boost/thread.hpp&gt;
  
  boost::mutex mutex;
  std::deque&lt;std::string&gt; queue;
  
  void producer() 
  {
      while (true) {
          boost::lock_guard&lt;boost::mutex&gt; lock(mutex);
  
          std::cout &lt;&lt; "producer() pushing string onto queue" &lt;&lt; std::endl;
  
          queue.push_back(std::string("test"));
      }
  }
  
  void consumer()
  {
      while (true) {
          boost::lock_guard&lt;boost::mutex&gt; lock(mutex);
  
          if (!queue.empty()) {
              std::cout &lt;&lt; "consumer() popped string " &lt;&lt; queue.front() &lt;&lt; " from queue" &lt;&lt; std::endl;
  
              queue.pop_front();
          }
      }
  }
  
  int main()
  {
      boost::thread producer_thread(producer);
      boost::thread consumer_thread(consumer);
  
      sleep(5);
  
      producer_thread.detach();
      consumer_thread.detach();
  
      return 0;
  }
  </code></pre>
  
  <p>This code runs as I expect, but when <code>main</code> exits, I get</p>
  
  <pre><code>/usr/include/boost/thread/pthread/mutex.hpp:45:    
      boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&amp;m)' failed.
  consumer() popped string test from queue
  Aborted
  </code></pre>
  
  <p>(I'm not sure if the output from <code>consumer</code> is relevant in that position, but I've left it in.)</p>
  
  <p>Am I doing something wrong in my usage of Boost? </p>