title: How to initialize a static variable in a multithreaded context?
body: <p>I thought up a good use of the static keyword inside a function to be something like this:</p>
  
  <pre><code>void threadSafeWrite(int *array, int writeIndex, int writeData){
      static void *threadLock = Lock_create(); //in my code locks are void* to be cross-platform compatable
      Lock_aquire(threadLock);
      array[writeIndex] = writeData;
      Lock_release(threadLock);
  }
  </code></pre>
  
  <p>In short it seems like a good way to make a critical section. My question is though, how do I initialize the threadLock in a threadsafe manner? The problem with the example I fear is that the lock will get allocated multiple times and each thread will use a different lock. Any ideas on how to fix this? Seems like a chicken and egg problem. I want a solution (or solutions) which work with both pthreads and windows threads. </p>
  
  <p>EDIT: the reason I want this functionality is because it provides a non-intrusive way to test whether there is a difference when running a piece of code single-threaded or multithreaded (meant for debug purposes).</p>