title: Sharing data between threads in C
body: <p>I have the following main.c:</p>
  
  <pre><code>#include &lt;unistd.h&gt; //has thread calls for fork()
  #include &lt;stdio.h&gt;
  
  struct globalInfo{
      int x;
  };
  
  int do this()
  {
      info.x++;
      printf("%d\n",info.x);
      return 0;
  }
  int main{
      struct globalInfo info = { .x = 2};
      for(int i = 0 ; i &lt; 5 ; i++)
      {
           if(fork() = 0)
           {
               dothis();
           }
      }
   }
  </code></pre>
  
  <p>This isn't my exact code, but my question is more easily demonstrated here.</p>
  
  <p>Now the output for the above function is:</p>
  
  <pre><code>3
  3
  3
  3
  3
  </code></pre>
  
  <p>What I want is:</p>
  
  <pre><code>3
  4
  5
  6
  7
  </code></pre>
  
  <p>How do share this struct between threads? It seems like every thread is just creating its own copy of the struct and manipulating its own copy. I've tried to pass a pointer to the info struct as a parameter to <code>dothis()</code>, but that doesn't fix it either. I've also tried placing the info initialization out of the main; that didn't work either..</p>
  
  <p>Help would be greatly appreciated.</p>