title: Per-thread state vs. shared state in threads
body: <p>I'm trying to understand the details in the TCB (thread control block and the differences between per-thread states and shared states. My book has its own implementation of pthread, so it gives an example with this mini C program (I've not typed the whole thing out)</p>
  
  <pre><code>#include "thread.h"
  static void go(int n);
  static thread_t threads[NTHREADS];
  #define NTHREADS 10
  
  int main(int argh, char **argv) {
      int i;
      long exitValue;
  
      for (i = 0; i &lt; NTHREADS; i++) {
          thread_create(&amp;threads[i]), &amp;go, i);
      }
  
      for (i = 0; i &lt; NTHREADS; i++) {
          exitValue = thread_join(threads[i]);
      }
  
      printf("Main thread done".\n);
      return 0;
  }
  
  void go(int n) {
      printf("Hello from thread %d\n", n);
      thread_exit(100 + n);
  }
  </code></pre>
  
  <p>What would the variables <code>i</code> and <code>exitValue</code> (in the <code>main()</code> function) be examples of? They're not shared state since they're not global variables, but I'm not sure if they're per-thread state either. The <code>i</code> is used as the parameter for the go function when each thread is being created, so I'm a bit confused about it. The exitValue's scope is limited only to <code>main()</code> so that seems like it would just be stored on the process' stack. The int <code>n</code> as the parameter for the <code>void go()</code> would be a per-thread variable because its value is independent for each thread. I don't think I fully understand these concepts so any help would be appreciated! Thanks!</p>