title: C threading (pthread_create) not working as expected
body: <p>I want to create multiple threads (10 in the example below) and have each of them run a function.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  typedef struct arg_struct {
      int id;
      int val;
  } arg_struct;
  
  void *printarg(void *params) {
      arg_struct *args = (arg_struct *) params;
      printf("id %i value %i\n", args-&gt;id, args-&gt;val);
      return 0;
  }
  
  int main() {
      int i = 0;
      pthread_t threads[10];
      for (i = 0; i &lt; 10; i++) {
          arg_struct arg;
          arg.id = i;
          arg.val = i + 10;
          pthread_create(&amp;(threads[i]), NULL, &amp;printarg, &amp;arg);
      }
      for (i = 0; i &lt; 10; i++) {
          pthread_join(threads[i], NULL);
      }
      return 0;
  }
  </code></pre>
  
  <p>I was expecting an output like this during execution:</p>
  
  <pre><code>id 0 value 10
  id 1 value 11
  id 2 value 12
  id 3 value 13
  id 4 value 14
  id 5 value 15
  id 6 value 16
  id 7 value 17
  id 8 value 18
  id 9 value 19
  </code></pre>
  
  <p>But instead I get outputs like this:</p>
  
  <pre><code>id 2 value 12
  id 3 value 13
  id 3 value 13
  id 4 value 14
  id 6 value 16
  id 6 value 16
  id 9 value 19
  id 9 value 19
  id 9 value 19
  id 9 value 19
  </code></pre>
  
  <p>Every time I rerun my program the output changes slightly. The id's &amp; value's repeat and the pattern seems irregular. </p>
  
  <p>What is happening? Are the repeats happening because the child threads are also creating threads in the for loop? If so, I am confused because in many of the examples of pthread_create I read, people seemed to be using a loop which iterates N times to create N threads (exactly like above). Therefore I assumed that the above code will have 1 master thread create N child threads? Am I wrong? Thanks in advance.</p>