title: Is it possible for multiple Dynamic Link Libraries (DLL) to share Thread Local Storage from a Static Library (LIB)
body: <p>I have a game made up of many DLL files. Some of these DLLs link to the same static library (LIB).</p>
  
  <p>So something like this:</p>
  
  <pre><code>Game.exe -&gt; Root.dll -&gt; Child.dll
                 |            |
                 |            '-&gt; Common.lib (contains __declspec(thread))
                 |
                 '-&gt; Common.lib (contains __declspec(thread))
  </code></pre>
  
  <p>Root.dll loads Child.dll which statically links Common.lib. Root also statically links Common.lib. Because Common is statically linked, it gets compiled directly into the loading dll (eg. Root and Child). </p>
  
  <p>Common.lib contains a variable using Thread Local Storage (TLS).</p>
  
  <pre><code>__declspec(thread) static void* s_threadValues[PlatformThreadSlotsMax];
  </code></pre>
  
  <p>This results in some problematic behavior: Root.dll and Child.dll each contain a <em>different</em> instance of the TLS data (s_threadValues). Even on the same thread, if Root.dll calls a function defined in Common.lib, the value of s_threadValues will be different from its value if that same function is called from Child.dll.</p>
  
  <p>Since both DLL are accessing this TLS from the same thread, I would expect the TLS to be shared, but it is not.</p>
  
  <p>Now, if I change Common.lib to a dynamically link (eg. Common.dll) this issue does <strong>not</strong> occur anymore: s_threadValues is the same for both Root.dll and Child.dll.</p>
  
  <p>Is this expected behavior? Is there anyway to have the TLS shared defined in the static lib shared between dynamic libs using it?</p>