title: pthread_mutex_lock __pthread_mutex_lock_full: Assertion failed with robust and 0x4000000
body: <p>I'm working on a server-side project, which is supposed to accept more than 100 client connections.</p>
  
  <p>It's multithreaded program using boost::thread.  Some places I'm using <code>boost::lock_guard&lt;boost::mutex&gt;</code> to lock the shared member data.  There is also a <code>BlockingQueue&lt;ConnectionPtr&gt;</code> which contains the input connections.  The implementation of the <code>BlockingQueue</code>:</p>
  
  <pre><code>template &lt;typename DataType&gt;
  class BlockingQueue : private boost::noncopyable
  {
  public:
      BlockingQueue()
          : nblocked(0), stopped(false)
      {
  
      }
  
      ~BlockingQueue()
      {
          Stop(true);
      }
  
      void Push(const DataType&amp; item)
      {
          boost::mutex::scoped_lock lock(mutex);
          queue.push(item);
          lock.unlock();
          cond.notify_one(); // cond.notify_all();
      }
  
      bool Empty() const
      {
          boost::mutex::scoped_lock lock(mutex);
          return queue.empty();
      }
  
      std::size_t Count() const
      {
          boost::mutex::scoped_lock lock(mutex);
          return queue.size();
      }
  
      bool TryPop(DataType&amp; poppedItem)
      {
          boost::mutex::scoped_lock lock(mutex);
          if (queue.empty())
              return false;
  
          poppedItem = queue.front();
          queue.pop();
  
          return true;
      }
  
      DataType WaitPop()
      {
          boost::mutex::scoped_lock lock(mutex);
  
          ++nblocked;
          while (!stopped &amp;&amp; queue.empty()) // Or: if (queue.empty())
              cond.wait(lock);
          --nblocked;
  
          if (stopped)
          {
              cond.notify_all(); // Tell Stop() that this thread has left
              BOOST_THROW_EXCEPTION(BlockingQueueTerminatedException());
          }
  
          DataType tmp(queue.front());
          queue.pop();
  
          return tmp;
      }
  
      void Stop(bool wait)
      {
          boost::mutex::scoped_lock lock(mutex);
          stopped = true;
          cond.notify_all();
  
          if (wait) // Wait till all blocked threads on the waiting queue to leave BlockingQueue::WaitPop()
          {
              while (nblocked)
                  cond.wait(lock);
          }
      }
  
  private:
      std::queue&lt;DataType&gt;          queue;
      mutable boost::mutex          mutex;
      boost::condition_variable_any cond;
      unsigned int                  nblocked;
      bool                          stopped;
  };
  </code></pre>
  
  <p>For each <code>Connection</code>, there is a <code>ConcurrentQueue&lt;StreamPtr&gt;</code>, which contains the input Streams.  The implementation of the <code>ConcurrentQueue</code>:</p>
  
  <pre><code>template &lt;typename DataType&gt;
  class ConcurrentQueue : private boost::noncopyable
  {
  public:
      void Push(const DataType&amp; item)
      {
          boost::mutex::scoped_lock lock(mutex);
          queue.push(item);
      }
  
      bool Empty() const
      {
          boost::mutex::scoped_lock lock(mutex);
          return queue.empty();
      }
  
      bool TryPop(DataType&amp; poppedItem)
      {
          boost::mutex::scoped_lock lock(mutex);
          if (queue.empty())
              return false;
  
          poppedItem = queue.front();
          queue.pop();
  
          return true;
      }
  private:
      std::queue&lt;DataType&gt; queue;
      mutable boost::mutex mutex;
  };
  </code></pre>
  
  <p>When debugging the program, it's okay.  But in a load testing with 50 or 100 or more client connections, sometimes it aborted with</p>
  
  <pre><code>pthread_mutex_lock.c:321: __pthread_mutex_lock_full: Assertion `robust || (oldval &amp; 0x40000000) == 0' failed.
  </code></pre>
  
  <p>I have no idea what happened, and it cannot be reproduced every time.</p>
  
  <p>I googled a lot, but no luck.  Please advise.</p>
  
  <p>Thanks.</p>
  
  <p>Peter</p>