title: when g++ static link pthread, cause Segmentation fault, why?
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;thread&gt;
  
  #define SIZE 1024
  #define AMOUNT 100000
  #define THREADS 4
  
  class A
  {
  private:
      char a[SIZE];
  };
  
  void test()
  {
      std::cout &lt;&lt; "test start\n";
      std::map&lt;int, A*&gt; container;
      for(int i=0; i&lt;AMOUNT; i++)
      {
          A* a = new A();
          std::pair&lt;int, A*&gt;p = std::make_pair(i, a);
          container.insert(p);
      }
  
      std::cout &lt;&lt; "test release\n";
      for(int i=0; i&lt;AMOUNT; i++)
      {
          auto iter = container.find(i);
          delete iter-&gt;second;
          container.erase(iter);
      }
      std::cout &lt;&lt; "test end\n";
  }
  
  int main()
  {
      std::thread ts[THREADS];
      for(int i=0; i&lt;THREADS; i++)
      {
          ts[i] = std::thread(test);
      }
  
      for(std::thread&amp; x: ts)
      {
          x.join();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Above is a simple c++ code.</p>
  
  <p>compile with: <code>g++ -pthread -o one one.cpp -Wall -std=c++11 -O3</code></p>
  
  <p><code>ldd one</code>, gots:</p>
  
  <pre><code>    linux-vdso.so.1 =&gt;  (0x00007ffebafce000)
      libstdc++.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb47352a000)
      libgcc_s.so.1 =&gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb473313000)
      libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb4730f4000)
      libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb472d2a000)
      libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb472a22000)
      /lib64/ld-linux-x86-64.so.2 (0x00005654c5112000)
  </code></pre>
  
  <p>run <code>./one</code>, every thing is ok.</p>
  
  <p>Then I try a static link: <code>g++ -pthread -o one one.cpp -Wall -std=c++11 -O3 -static</code></p>
  
  <p><code>ldd one</code>, gots:</p>
  
  <pre><code>    not a dynamic executable
  </code></pre>
  
  <p>But when I run it, some thing goes wrong...</p>
  
  <pre><code>test start
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>re-compile with <code>-g</code>, and the gdb shows:</p>
  
  <pre><code>wang[00:35][~/test]$ gdb one
  GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10
  Copyright (C) 2015 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-linux-gnu".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  Find the GDB manual and other documentation resources online at:
  &lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from one...done.
  (gdb) run
  Starting program: /home/wang/test/one 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  [New Thread 0x7ffff7ffa700 (LWP 3623)]
  test start
  [New Thread 0x7ffff77f8700 (LWP 3624)]
  test start
  [New Thread 0x7ffff6ff7700 (LWP 3625)]
  test start
  [New Thread 0x7ffff67f6700 (LWP 3626)]
  test start
  
  Program received signal SIGSEGV, Segmentation fault.
  0x0000000000000000 in ?? ()
  (gdb) 
  </code></pre>
  
  <p>Why this ?</p>
  
  <p><strong>UPDATE</strong>
  <strong>==============================</strong></p>
  
  <p>using <code>boost::thread</code> library (boost version: 1.60), </p>
  
  <p>replace <code>std::thread</code> with <code>boost::thread</code> ,  and make a static link,  </p>
  
  <p><code>g++ -pthread -o one1 one.cpp -Wall -std=c++11 -O3 -I /opt/boost/include/ -L /opt/boost/lib/ -lboost_system -lboost_thread -static</code></p>
  
  <p>no problem occurred!</p>
  
  <p>confused...</p>