title: Static Variables and Threads (C)
body: <p>I know that declaring a static variable within a function in C means that this variable retains its state between function invocations. In the context of threads, will this result in the variable retaining its state over multiple threads, or having a separate state <em>between each thread</em>? </p>
  
  <p>Here is a past paper exam question I am struggling to answer:</p>
  
  <blockquote>
    <p><strong>The following C function is intended to be used to allocate unique identifiers (UIDs)  to its callers:</strong></p>
  
  <pre><code>get_uid() 
  {
  static int i = 0;
  return i++;
  }
  </code></pre>
    
    <p><strong>Explain in what way get_uid() might work incorrectly in an environment where it is  being called by multiple threads. Using a
    specific example scenario, give specific  detail on why and how such
    incorrect behaviour might occur.</strong></p>
  </blockquote>
  
  <p>At the moment I am assuming that each thread has a separate state for the variable, but I am not sure if that is correct or if the answer is more to do with the lack of mutual exclusion. If that is the case then how could semaphores be implemented in this example?</p>