title: Valgrind errors in c++ threads
body: <p>I am new to Valgrind. It throws 2 errors during memory check for the following code (<a href="http://en.cppreference.com/w/cpp/thread/lock_guard" rel="noreferrer">http://en.cppreference.com/w/cpp/thread/lock_guard</a>). Not sure how to interpret those errors. </p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;iostream&gt;
  
  int g_i = 0;
  std::mutex g_i_mutex;  // protects g_i
  
  void safe_increment()
  {
      std::lock_guard&lt;std::mutex&gt; lock(g_i_mutex);
      ++g_i;
      std::cout &lt;&lt; std::this_thread::get_id() &lt;&lt; ": " &lt;&lt; g_i &lt;&lt; '\n';
  }
  
  int main()
  {
      std::cout &lt;&lt; __func__ &lt;&lt; ": " &lt;&lt; g_i &lt;&lt; '\n';
      std::thread t1(safe_increment);
      std::thread t2(safe_increment);
      t1.join();
      t2.join();
     std::cout &lt;&lt; __func__ &lt;&lt; ": " &lt;&lt; g_i &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>I compiled this code on Mac using the following command:</p>
  
  <pre><code>clang++ simplethread.cpp -o simplethread -lpthread -std=c++11
  </code></pre>
  
  <p>The code runs as expected, but I get the following output from Valgrind.</p>
  
  <pre><code>$ valgrind --tool=memcheck ./simplethread
  ==34831== Memcheck, a memory error detector
  ==34831== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==34831== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  ==34831== Command: ./simplethread
  ==34831== 
  ==34831== Syscall param msg-&gt;desc.port.name points to uninitialised byte(s)
  ==34831==    at 0x10045F34A: mach_msg_trap (in         /usr/lib/system/libsystem_kernel.dylib)
  ==34831==    by 0x10045E796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
  ==34831==    by 0x100458485: task_set_special_port (in  /usr/lib/system/libsystem_kernel.dylib)
  ==34831==    by 0x1005F410E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
  ==34831==    by 0x1005F4458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
  ==34831==    by 0x1000A99DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
  ==34831==    by 0x10001BA1A:    ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&amp;) (in  /usr/lib/dyld)
  ==34831==    by 0x10001BC1D:   ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&amp;) (in  /usr/lib/dyld)
  ==34831==    by 0x1000174A9:   ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&amp;, unsigned int, char const*, ImageLoader::InitializerTimingList&amp;,    ImageLoader::UninitedUpwards&amp;) (in /usr/lib/dyld)
  ==34831==    by 0x100017440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&amp;, unsigned int, char const*, ImageLoader::InitializerTimingList&amp;, ImageLoader::UninitedUpwards&amp;) (in /usr/lib/dyld)
  ==34831==    by 0x100016523: ImageLoader::processInitializers(ImageLoader::LinkContext const&amp;, unsigned int, ImageLoader::InitializerTimingList&amp;, ImageLoader::UninitedUpwards&amp;) (in /usr/lib/dyld)
  ==34831==    by 0x1000165B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&amp;, ImageLoader::InitializerTimingList&amp;) (in /usr/lib/dyld)
  ==34831==  Address 0x10488ed4c is on thread 1's stack
  ==34831==  in frame #2, created by task_set_special_port (???:)
  ==34831== 
  main: 0
  ==34831== Thread 2:
  ==34831== Invalid read of size 4
  ==34831==    at 0x1005BC899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==    by 0x1005BC886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==    by 0x1005BC08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
  ==34831== 
  ==34831== 
  ==34831== Process terminating with default action of signal 11 (SIGSEGV)
  ==34831==  Access not within mapped region at address 0x18
  ==34831==    at 0x1005BC899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==    by 0x1005BC886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==    by 0x1005BC08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
  ==34831==  If you believe this happened as a result of a stack
  ==34831==  overflow in your program's main thread (unlikely but
  ==34831==  possible), you can try to increase the size of the
  ==34831==  main thread stack using the --main-stacksize= flag.
  ==34831==  The main thread stack size used in this run was 8388608.
  --34831:0:schedule VG_(sema_down): read returned -4
  ==34831== 
  ==34831== HEAP SUMMARY:
  ==34831==     in use at exit: 22,494 bytes in 168 blocks
  ==34831==   total heap usage: 184 allocs, 16 frees, 28,638 bytes allocated
  ==34831== 
  ==34831== LEAK SUMMARY:
  ==34831==    definitely lost: 16 bytes in 1 blocks
  ==34831==    indirectly lost: 56 bytes in 2 blocks
  ==34831==      possibly lost: 72 bytes in 3 blocks
  ==34831==    still reachable: 4,368 bytes in 10 blocks
  ==34831==         suppressed: 17,982 bytes in 152 blocks
  ==34831== Rerun with --leak-check=full to see details of leaked memory
  ==34831== 
  ==34831== For counts of detected and suppressed errors, rerun with: -v
  ==34831== Use --track-origins=yes to see where uninitialised values come from
  ==34831== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
  Segmentation fault: 11
  </code></pre>
  
  <p>What does these errors mean ?  </p>