title: C++ multithreading: terminate after throwing an instance of 'std::length_error'
body: <p>I'm learning multithreading in C++ using pthread on <a href="http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm" rel="nofollow">Tutorials Point</a> (it might be old but I just need something to get started). I modified the code a little bit:</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #define NUM_THREADS 5
  #define DEBUG
  #undef DEBUG
  
  using namespace std;
  
  struct thread_data {
      int thread_id = 0;
      string message = "";
  };
  
  void *printHello(void *threadData)
  {
      struct thread_data* data = (struct thread_data*)threadData;
      int thread_id = data-&gt;thread_id;
      string message = data-&gt;message;
  
      cout &lt;&lt; "From thread " &lt;&lt; thread_id &lt;&lt; ": " &lt;&lt; message &lt;&lt; "\n";
      pthread_exit(NULL);
  }
  
  int main(int argc, char** argv)
  {
      pthread_t threads[NUM_THREADS];
  
      for (int i = 0; i &lt; NUM_THREADS; i++) {
          struct thread_data* data = new thread_data();
          string message = "Special message for thread #" + to_string(i) + "!";
  
          #ifdef DEBUG
          cout &lt;&lt; "DEBUG: " &lt;&lt; "i = " &lt;&lt; i &lt;&lt; endl;
          #endif
  
          data-&gt;thread_id = i;
          data-&gt;message = message;
  
          cout &lt;&lt; "main(): creating thread, " &lt;&lt; i &lt;&lt; endl;
          int rc = 0;
          rc = pthread_create(&amp;threads[i], NULL, printHello, (void *) data);
  
          delete data;
  
          if (rc) {
              cout &lt;&lt; "Error: unable to create thread: " &lt;&lt; rc &lt;&lt; "\n";
              return -1;
          }
      }
  
      pthread_exit(NULL);    
  }
  </code></pre>
  
  <p>I compiled with:</p>
  
  <pre><code>g++ -pthread -g -Wall -std=c++11 main.cpp -o main
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>main(): creating thread, 0
  main(): creating thread, 1
  From thread 1: Special message for thread #1!
  main(): creating thread, 2
  From thread 2: Special message for thread #2!
  main(): creating thread, 3
  From thread 3: Special message for thread #3!
  main(): creating thread, 4
  From thread 4: Special message for thread #4!
  terminate called after throwing an instance of 'std::length_error'
    what():  basic_string::_S_create
  Aborted (core dumped)
  </code></pre>
  
  <p>If I don't create the threads using <code>pthread_create</code> and call the printHello function directly, no error occurs. Sometimes, the program would throw a segfault instead, and sometimes it ran smoothly!</p>
  
  <p>Another problem is that, there should be a line saying <code>From thread 0: Special message for thread #0!</code> from the beginning, but there was not.</p>
  
  <p>Moreover, sometimes the message "Special message for thread #n!" did not even appear, at all.</p>
  
  <p>I tried initializing the struct's variables, use statically allocated memory (stack instead of heap by not using <code>new</code>) at line 31. I tried avoiding to use pointers in the printHello function, but since the last argument of pthread_create only accepts a pointer to the function's arguments, I couldn't do that.</p>
  
  <p>My first suspicion was that there is something wrong when I assign <code>data-&gt;message = message</code>, so I tried assigning the string directly to <code>data-&gt;message</code>, but no luck. But I still think the error has to be there, since the exception is <code>std::length_error</code>, thrown by 'basic_string`. </p>
  
  <p>Or, maybe when I pass <code>data</code> to <code>pthread_create</code> or when I make the cast at line 18, I did something wrong. My thought is that when I pass it to the function, I pass it as a pointer, cast it to a void pointer <code>void *</code>. When <code>printHello</code> received the parameter, I cast it to type <code>thread_data*</code>, which is a pointer, which is what it originally is.</p>
  
  <p>That's when I'm able to come up with so far. Please comment if there's something obscure in my writing (English is not my first language).</p>
  
  <p>Thanks everyone in advance.</p>