title: Thread return value is inconsistent with expected output
body: <p>I'm working on a project that requires somewhat similar functionality to the program below so I've tried to create a simpler program to debug my larger program. The threads I'm creating are returning values that are inconsistent with their expected output but their return values are not random. It almost seems like the threads are returning the values from other threads or that the variable they are returned to ("tmp") is updating.</p>
  
  <p>The expected output should be...</p>
  
  <p>0 1</p>
  
  <p>1 2</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  struct Numbers {
      int x;
      int y;
  };
  
  void *go(void* param)
  {
      struct Numbers* nums = (struct Numbers*) param;
      int sum = nums -&gt; x + nums -&gt; y;
  
      return (void*) sum;
  }
  
  int main()
  {
      int result[2][2];
      int tmp;
  
      pthread_t thread[2][2];
  
      int i, j;
      for(i=0;i&lt;2;i++)
      {
          for(j=0;j&lt;2;j++)
          {
              struct Numbers nums;
              nums.x = i;
              nums.y = j;
  
              pthread_create(&amp;thread[i][j], NULL, go, &amp;nums);
          }
      }
  
      for(i=0;i&lt;2;i++)
      {
          for(j=0;j&lt;2;j++)
          {
              pthread_join(thread[i][j], (void*) &amp;tmp);
              result[i][j] = tmp;
          }
      }
  
      for(i=0;i&lt;2;i++)
      {
          for(j=0;j&lt;2;j++)
          {
              printf("%d\t", result[i][j]);
          }
          printf("\n");
      }
  
      return 0;
  }
  </code></pre>