title: How do I create a global variable that is thread-specific in C using POSIX threads?
body: <p>I am learning about POSIX threads and I have come to the section on Thread Specific Data. The book does an excellent example using a file descriptor. However, I wanted to do the same example on my own, except this time using a global variable. However, I am having some difficulty fully grasping this concept. </p>
  
  <p>What I want to do is the following:</p>
  
  <ul>
  <li>Create a global integer </li>
  <li>Declare a key for the global int</li>
  </ul>
  
  <p>in main:</p>
  
  <ul>
  <li>set global integer to be a value eg. 10</li>
  <li>create a key for it without any clean up</li>
  <li>create 4 threads and send them to execute thread_func</li>
  <li>check if value is still 10, since threads only see a copy of it</li>
  </ul>
  
  <p>in thread_func:</p>
  
  <ul>
  <li>use pthread_setspecific(key,global variable) to create a local instance - not sure if I am   interpreting this correctly </li>
  <li>call a function - dosomething()</li>
  <li>exit</li>
  </ul>
  
  <p>in do_something</p>
  
  <ul>
  <li>create a local pointer and assign it to pthread_getspecific(key) - this should get me a thread specific version of the global variable</li>
  <li>change the value of what is stored at the local pointer to 2</li>
  <li>exit </li>
  </ul>
  
  <p>Here's the code:</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define NUMTHREADS 4
  
  pthread_key_t glob_var_key;
  int glob_var;
  
  void do_something()
  {
      //get thread specific data
      int* glob_spec_var = (int*) pthread_getspecific(glob_var_key);
      printf("Thread %d glob_spec before mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
      *glob_spec_var = 2;
      printf("Thread %d glob_spec after mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
  }
  
  void* thread_func(void *arg)
  {
      pthread_setspecific(glob_var_key, &amp;glob_var);
      do_something();
      pthread_exit(NULL);
  }
  
  int main(void)
  {
      pthread_t threads[NUMTHREADS];
      int i;
      glob_var = 10;
      pthread_key_create(&amp;glob_var_key,NULL);
      printf("Main: glob_var is %d\n", glob_var);
      for (i=0; i &lt; NUMTHREADS; i++)
      {
          pthread_create(&amp;threads[i],NULL,thread_func,NULL);
      }
  
      for (i=0; i &lt; NUMTHREADS; i++)
      {
          pthread_join(threads[i], NULL);
      }
      printf("Main: glob_var is %d\n", glob_var);
  
      return 0;
  }
  </code></pre>
  
  <p>From what I understood, when you call pthread_getspecific, each thread is supposed to have its own unique memory address for the memory addresses - which I did not find to be the case here. I know I am not going about this correctly and when I did try to look at the memory addresses for each thread when doing getspecific, I saw the same memory address. Perhaps someone can point me to an example where they use a global variable (not file descriptors) and they have thread-specific usage in which threads look at it as a local variable. </p>