title: valgrind drd reports errors for a simple c++ program w/ boost::thread or pthread
body: <p>I just copy an example program from boost 
  <a href="http://www.boost.org/doc/libs/1_31_0/libs/thread/example/thread_group.cpp" rel="nofollow">http://www.boost.org/doc/libs/1_31_0/libs/thread/example/thread_group.cpp</a></p>
  
  <pre><code>#include &lt;boost/thread/thread.hpp&gt;
  #include &lt;iostream&gt;
  
  int shared_counter = 0;
  boost::mutex mutex;
  
  void increment_count()
  {
      boost::mutex::scoped_lock lock(mutex);
      std::cout &lt;&lt; "count = " &lt;&lt; ++shared_counter &lt;&lt; std::endl;
  }
  
  int main(int argc, char* argv[])
  {
      boost::thread_group threads;
      for (int i = 0; i &lt; 10; ++i)
          threads.create_thread(&amp;increment_count);
      threads.join_all();
  }
  </code></pre>
  
  <p>and run it with </p>
  
  <pre><code>valgrind --tool=drd thread_group
  </code></pre>
  
  <p>and it reports many errors shown bellow:</p>
  
  <pre><code>==1454== Thread 2:
  ==1454== Conflicting load by thread 2 at 0x100843650 size 4
  ==1454==    at 0x1006FD94E: putc (in /usr/lib/libSystem.B.dylib)
  ==1454==    by 0x1005C0A18: std::ostream::put(char) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==1454==    by 0x1005C0AC6: std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::endl&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==1454==    by 0x10001BD32: increment_count() (libfbj.cpp:23)
  ==1454==    by 0x10001CCD6: boost::detail::thread_data&lt;void (*)()&gt;::run() (thread.hpp:62)
  ==1454==    by 0x10053DF17: thread_proxy (in /opt/local/lib/libboost_thread.dylib)
  ==1454==    by 0x1001005A5: vgDrd_thread_wrapper (drd_pthread_intercepts.c:341)
  ==1454==    by 0x1006AEFD5: _pthread_start (in /usr/lib/libSystem.B.dylib)
  ==1454==    by 0x1006AEE88: thread_start (in /usr/lib/libSystem.B.dylib)
  ==1454== Allocation context: Data section of /usr/lib/libSystem.B.dylib
  ==1454== Other segment start (thread 1)
  ==1454==    at 0x100102286: pthread_mutex_unlock (drd_pthread_intercepts.c:665)
  ==1454==    by 0x10001EBDE: boost::mutex::unlock() (mutex.hpp:72)
  ==1454==    by 0x10001EE5C: boost::unique_lock&lt;boost::mutex&gt;::~unique_lock() (locks.hpp:403)
  ==1454==    by 0x10001F097: boost::shared_mutex::lock() (shared_mutex.hpp:137)
  ==1454==    by 0x10001F10E: boost::lock_guard&lt;boost::shared_mutex&gt;::lock_guard(boost::shared_mutex&amp;) (locks.hpp:257)
  ==1454==    by 0x10001F751: boost::thread* boost::thread_group::create_thread&lt;void (*)()&gt;(void (*)()) (thread_group.hpp:41)
  ==1454==    by 0x10001BC3F: main (libfbj.cpp:30)
  ==1454== Other segment end (thread 1)
  ==1454==    at 0x1006AEE52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
  ==1454==    by 0x100106D8E: pthread_create (drd_pthread_intercepts.c:457)
  ==1454==    by 0x10053D78A: boost::thread::start_thread() (in /opt/local/lib/libboost_thread.dylib)
  ==1454==    by 0x10001F31A: boost::thread::thread&lt;void (*)()&gt;(void (*)(), boost::disable_if&lt;boost::is_convertible&lt;void (*&amp;)(), boost::detail::thread_move_t&lt;void (*)()&gt; &gt;, boost::thread::dummy*&gt;::type) (thread.hpp:205)
  ==1454==    by 0x10001F771: boost::thread* boost::thread_group::create_thread&lt;void (*)()&gt;(void (*)()) (thread_group.hpp:42)
  ==1454==    by 0x10001BC3F: main (libfbj.cpp:30)
  ...
  ==1454== ERROR SUMMARY: 49 errors from 6 contexts (suppressed: 215 from 39)
  </code></pre>
  
  <p>I use Mac OS X 10.6.8, Xcode 3.2.4 64-bit, gcc 4.2.1, project setting "Other C++ FLAGs" -pthread, valgrind 3.7.0 installed by "sudo port install valgrind", and boost 1.49 compiled w/</p>
  
  <pre><code>sudo ./bjam variant=debug define=BOOST_LOG_USE_CHAR architecture=combined address-model=32_64 link=shared,static threading=multi install
  </code></pre>
  
  <p>I also have tried pthread, here is the program</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;iostream&gt;
  
  #define NUM_THREADS 5
  
  pthread_mutex_t m_mutex;
  
  void *TaskCode(void *argument)
  {
      pthread_mutex_lock(&amp;m_mutex);
      std::cout &lt;&lt; "Hello World! It's me, thread " &lt;&lt; *((int *) argument) &lt;&lt; std::endl;
      pthread_mutex_unlock(&amp;m_mutex);
      return NULL;
  }
  
  int main(void)
  {
      pthread_mutex_init(&amp;m_mutex,0);
      pthread_t threads[NUM_THREADS];
      int thread_args[NUM_THREADS];
      int rc, i;
  
      // create all threads 
      for (i=0; i&lt;NUM_THREADS; ++i) {
          thread_args[i] = i;
          rc = pthread_create(&amp;threads[i], NULL, TaskCode, (void *) &amp;thread_args[i]);
          assert(0 == rc);
      }
  
      // wait for all threads to complete
      for (i=0; i&lt;NUM_THREADS; ++i) {
          rc = pthread_join(threads[i], NULL);
          assert(0 == rc);
      }
  
      pthread_mutex_destroy(&amp;m_mutex);
      return 0;
  }
  </code></pre>
  
  <p>Then "g++ -pthread -lpthread threaded_group.cpp -o threaded_group"
  and "valgrind --tool=drd --dsymutil=yes ./threaded_group"
  and get erors:</p>
  
  <pre><code>==71472== Thread 2:
  ==71472== Conflicting load by thread 2 at 0x1001fd650 size 4
  ==71472==    at 0x1000933DB: fwrite (in /usr/lib/libSystem.B.dylib)
  ==71472==    by 0x1002ADAE3: void std::__ostream_write&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==71472==    by 0x1002ABB97: std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::__ostream_insert&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==71472==    by 0x1002ABC57: std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==71472==    by 0x100000C46: TaskCode(void*) (in ./threaded_group)
  ==71472==    by 0x1000145A5: vgDrd_thread_wrapper (drd_pthread_intercepts.c:341)
  ==71472==    by 0x100068FD5: _pthread_start (in /usr/lib/libSystem.B.dylib)
  ==71472==    by 0x100068E88: thread_start (in /usr/lib/libSystem.B.dylib)
  ==71472== Allocation context: Data section of /usr/lib/libSystem.B.dylib
  ==71472== Other segment start (thread 1)
  ==71472==    at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ==71472== Other segment end (thread 1)
  ==71472==    at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
  ==71472==    by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ==71472== Other segment start (thread 1)
  ==71472==    at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ==71472== Other segment end (thread 1)
  ==71472==    at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
  ==71472==    by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ==71472== Other segment start (thread 1)
  ==71472==    at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ==71472== Other segment end (thread 1)
  ==71472==    at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
  ==71472==    by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
  ==71472==    by 0x100000B70: main (in ./threaded_group)
  ...
  ==71472== ERROR SUMMARY: 11 errors from 6 contexts (suppressed: 70 from 29)
  </code></pre>
  
  <p>can anyone tell me what's wrong?</p>