title: How to spawn n threads?
body: <p>I'm trying to write a multi-threaded program, the number of threads based on command-line input, and so I can't hard-code pre-declared threads. Is this a valid way of doing it?</p>
  
  <pre><code>int threads = 5; // (dynamic, not hard-coded)
  int i = 0;
  pthread_t * thread = malloc(sizeof(pthread_t)*threads);
  
  for (i = 0; i &lt; threads; i++) {
      pthread_t foobar;
      thread[i] = foobar; // will this cause a conflict?
  }
  
  for (i = 0; i &lt; threads; i++) {
  
      int ret = pthread_create(&amp;thread[i], NULL, (void *)&amp;foobar_function, NULL);
  
      if(ret != 0) {
          printf ("Create pthread error!\n");
          exit (1);
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Here's my result from modifications suggested below. Seems to work just fine.</p>
  
  <pre><code>int threads = 5;
  int i;
  
  pthread_t * thread = malloc(sizeof(pthread_t)*threads);
  
  for (i = 0; i &lt; threads; i++) {
  
      int ret = pthread_create(&amp;thread[i], NULL, &amp;foobar_function, NULL);
  
      if(ret != 0) {
          printf ("Create pthread error!\n");
          exit (1);
      }
      // pthread_join(thread[i], NULL); // don't actually want this here :)
  }
  
  sleep(1);     // main() will probably finish before your threads do,
  free(thread); // so we'll sleep for illustrative purposes
  </code></pre>