title: study for exam, looking for explanation. pthread_join. what is this code doing?
body: <p>I am looking at this piece of code:</p>
  
  <pre><code>/* 04-join.c
  
  Simple illustration of dividing work among two threads
  
  pthread.h should be first otherwise compile with -D_THREAD_SAFE 
  */
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct {
     int *ar;
     long n;
     int id;
     int sum;
  } subarray;
  
  
  void *incer(void *arg) {
    long i;
     int id = ((subarray*)arg)-&gt;id;
  
     printf("Start thread: %d sum=%ld\n",id, ((subarray*)arg)-&gt;sum);
     for (i = 0; i &lt; ((subarray*)arg)-&gt;n; i++) {
        ((subarray*)arg)-&gt;ar[i]++;
        ((subarray*)arg)-&gt;sum++;
     }
     printf("Terminating thread: %d sum=%ld\n",id, ((subarray*)arg)-&gt;sum);
     pthread_exit(NULL);
  }
  
  int main() {
     int ar[1000000];
     pthread_t th1, th2;
     subarray sb1, sb2;
  
     sb1.ar = &amp;ar[0];
     sb1.n  = 500000;
     sb1.id = 1;
     sb1.sum = 0;
     pthread_create(&amp;th1, NULL, incer, &amp;sb1);
  
     sb2.ar = &amp;ar[500000];
     sb2.n  = 500000;
     sb2.id = 2;
     sb2.sum = 0;
     pthread_create(&amp;th2, NULL, incer, &amp;sb2);
  
     pthread_join(th1, NULL);
     pthread_join(th2, NULL);
     printf("Terminating main: %d sum=%d\n",getpid(),sb1.sum+sb2.sum);
     pthread_exit(NULL);
     return 0;
  }
  </code></pre>
  
  <p>My question is, in the function incer in the for loop. We have the piece of code </p>
  
  <pre><code>((subarray*)arg)-&gt;ar[i]++;
  </code></pre>
  
  <p>In the subarry struct ar is a pointer to a integer, correct? So what does ar[i]++ do? Why is it being done? Is it simulating "work done" by this thread, along with incrementing the sum?</p>
  
  <p>Also one more small question. What is the purpose of "void *" in front of incer? I missed the part about the importance of void *. </p>
  
  <p>Does it mean that, this pointer points to the address of the incer function. And by creating the function a void * called "incer" is created and we can use it to refer to this section of code when creating the thread, so it knows to execute this section of code? </p>
  
  <p>Also, why pthreads? process-threads?</p>