title: pthread_create in constructor segfault
body: <p>I discovered, in this little example below, if I call pthread_create in the constructor of my struct, I get a segfault randomly on the call to pthread_mutex_lock(). </p>
  
  <p>And sometimes the name field is empty for the first philosopher.  </p>
  
  <p>If I move pthread_create to a run() function after the constructor, no segfault.</p>
  
  <p>It seems the call to pthread_create happens before all the members are initialized.  Shouldn't the member init list of the class be completed before the call to constructor body? </p>
  
  <p>Thanks for any tips! </p>
  
  <p>clang version 9.0.0 (tags/RELEASE_900/final)
  Target: x86_64-apple-darwin17.7.0</p>
  
  <p>Sincerely, George </p>
  
  <pre><code>#include &lt;array&gt;
  #include &lt;iostream&gt;
  #include &lt;pthread.h&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  
  struct chopstick
  {
          pthread_mutex_t mutex;
          chopstick()
          {
                  pthread_mutex_init(&amp;mutex,nullptr);
          }
          ~chopstick()
          {
                  pthread_mutex_destroy(&amp;mutex);
          }
  
  };
  
  void* feed(void* data);
  struct philosopher
  {
          pthread_t thread;
          string name;
          unsigned mouthfuls;
          chrono::seconds sec;
          chopstick &amp;left, &amp;right;
  
          pthread_t&amp; get_thread() { return thread; }
  
          philosopher(const string &amp;s, chopstick &amp;l, chopstick &amp;r): name(move(s)), left(l), right(r), mouthfuls(0)
          /*
                  enable below to avoid segfault
          {}
          void run()  
          */
          {
                  pthread_create(&amp;thread, nullptr, feed, this);
          };
  
  };
  
  void* feed(void* data)
  {
          philosopher &amp; a = *static_cast&lt;philosopher*&gt;(data);
          while (a.mouthfuls &lt; 20)
          {
                  pthread_mutex_lock(&amp;a.left.mutex);
                  pthread_mutex_lock(&amp;a.right.mutex);
                  cout &lt;&lt; "Apostle " &lt;&lt; a.name &lt;&lt; " thread id " &lt;&lt; pthread_self() 
                          &lt;&lt; " acquired a chopstick at count: " &lt;&lt; a.mouthfuls &lt;&lt; endl;
                  ++a.mouthfuls;
                  pthread_mutex_unlock(&amp;a.right.mutex);
                  pthread_mutex_unlock(&amp;a.left.mutex);
          }
          return nullptr;
  }
  
  int main (int argc, char const * argv[])
  {
          array&lt;string, 12&gt; names {"John", "Thaddeus", "Simon Peter", "James", "Judhas", "Bartholomew", "Matthew", "Philip", "Simon Zealot", "Thomas", "Andrew", "James the Lesser" };
          array&lt;chopstick,names.size()&gt; sticks;
          vector&lt;philosopher&gt; philosophers;
  
          for (int i=0; i+1&lt;names.size(); ++i)
                  philosophers.emplace_back( names[i],sticks[i],sticks[i+1] );
          philosophers.emplace_back(names[names.size()-1], sticks[0],sticks[names.size()-1]);
          //for (philosopher&amp;  a: philosophers) a.run();  //&lt;-- enable to avoid segfault
          for (philosopher&amp;  a: philosophers) pthread_join(a.get_thread(), nullptr);
  
          return 0;
  
  }
  </code></pre>