title: thread example, segmentation fault
body: <p>I wrote a simple C++ code to find the minimal value of a vector, find below. It compiles both on VC++ and g++, but runs onto a segmentation fault on the latter. I cannot tell apart if my code contains an UB or the g++ contains a bug. Can someone identify any mistake in my code?</p>
  <p>The segfault arises at thread::join().</p>
  <h1>some debugging info</h1>
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x0000000000000000 in ?? ()
  (gdb) where
  #0  0x0000000000000000 in ?? ()
  #1  0x00000000004688f7 in std::thread::join() ()
  #2  0x0000000000000000 in ?? ()
  (gdb) thread
  [Current thread is 1 (Thread 0x7c6880 (LWP 24015))]
  </code></pre>
  <h1>Here is the code</h1>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;random&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt; 
  using namespace std;
  
  void find_min(vector&lt;double&gt;&amp; x, double&amp; min_val, int&amp; min_id)
  {
      min_id = distance(x.begin(), min_element(x.begin(), x.end()));
      min_val = x[min_id];
  }
  
  void find_part_min(vector&lt;double&gt;&amp; x, vector&lt;int&gt;&amp; min_ids, vector&lt;double&gt;&amp; min_vals, int id)
  {
      int start_id = (x.size()*id) / min_vals.size();
      int end_id = (x.size()*(id + 1)) / min_vals.size();
      for (int i = start_id; i &lt; end_id; ++i)
      {
          if (x[i] &lt; min_vals[id])
          {
              min_ids[id] = i;
              min_vals[id] = x[i];
          }
      }
  }
  
  
  int main()
  {
      // define variables
      int Nthreads = 16;
      vector&lt;double&gt; x(256 * 256);
      int min_id = 0;
      double min_val = 0;
  
      // fill up vector with random content
      mt19937 gen(0);
      uniform_real_distribution&lt;&gt; dis(0, 1);
      generate(x.begin(), x.end(), bind(dis,gen));
      
      // find min serial
      find_min(x, min_val, min_id);
      cout &lt;&lt; min_id &lt;&lt; &quot;\t&quot; &lt;&lt; min_val &lt;&lt; endl;
      
      // initilaize variables for parallel computing
      vector&lt;double&gt; min_vals(Nthreads, numeric_limits&lt;double&gt;::infinity());
      vector&lt;int&gt; min_ids(Nthreads, -1);
      vector&lt;thread&gt; myThreads;
  
      for (int id = 0; id &lt; Nthreads; ++id) // define each thread
      {
          thread myThread(find_part_min, ref(x), ref(min_ids), ref(min_vals), id);
          myThreads.push_back(move(myThread));
      }
      for (int id = 0; id &lt; Nthreads; ++id)
          myThreads[id].join(); // part-calculations are finished
  
      // merging the results together
      min_val = numeric_limits&lt;double&gt;::infinity();
      min_id = -1;
      for (int i = 0; i &lt; Nthreads; ++i)
      {
          if (min_vals[i] &lt; min_val)
          {
              min_val = min_vals[i];
              min_id = min_ids[i];
          }
      }
  
      cout &lt;&lt; min_id &lt;&lt; &quot;\t&quot; &lt;&lt; min_val &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>