title: Global variable in namespace - Values differ in threads
body: <p>Consider the following szenario: </p>
  
  <ul>
  <li>2 different network-ports via <code>boost::asio</code> each in its own thread</li>
  <li>1 port is receiving and processing data - <code>class DataConnection</code> wrapped in a <code>std::thread</code></li>
  <li>1 port is for sending statistics <code>class StatConnection</code> also wrapped in a <code>std::thread</code> </li>
  </ul>
  
  <p>For counting connections (and other small data pieces) my idea was to use a <code>static</code> variable inside a <code>namespace</code> like: </p>
  
  <pre><code>#include &lt;atomic&gt;
  
  namespace app {
   namespace status {
     static std::atomic&lt;long&gt; counter = 0; 
   }
  }
  </code></pre>
  
  <p>This works fine for the <code>DataConnection</code> class. Here I increment <code>counter</code> in the c'tor and see the value increments. </p>
  
  <p>But <code>counter</code> in my <code>StatConnection</code> class is always <code>0</code> </p>
  
  <p>Why can this happen? </p>
  
  <p>I've tried some alternatives:</p>
  
  <ul>
  <li>exchanging <code>std::atomic&lt;long&gt;</code> for <code>static volatile long</code>:  Did not made a difference. </li>
  <li>using the namespace without <code>static</code> keyword.</li>
  </ul>
  
  <p>Then I got linker errors: </p>
  
  <pre><code>multiple definition of `app::status::searchtime'
  ./src/status/Status.o:/[...]/include/status/Status.hpp:16: first defined here
  [...]
  </code></pre>
  
  <p>So why is the value of <code>count</code> different between threads? </p>