title: std::thread blocks in a singleton and .dll
body: <p>I've read a lot about this, but I haven't found any solution yet.</p>
  
  <p>My problem is that when I try to do this, I loose control.</p>
  
  <p><code>hiloEscucha = std::thread(&amp;PlayerClientSingleton::Run, this);</code></p>
  
  <p>I'm working with a class implementing the singleton pattern in a multithread environment. My project is compiled to a DLL library. Everything works fine except thread creation.</p>
  
  <p>I've tried this (<a href="http://social.msdn.microsoft.com/forums/vstudio/en-US/6c81ad8e-6040-4889-92c8-61f7e678f562/static-singleton-never-recovers-from-member-thread-join" rel="nofollow">link</a>) and this example works fine in a stand alone console app, but my project is running as a DLL library.</p>
  
  <p>When I load it from other C++ and C# projects it works fine, and when calling other methods it works well too. The problem occurs whe I call <code>std::thread</code>.</p>
  
  <p>Here is the most important pieces of my code:</p>
  
  <pre><code>class PlayerClientSingleton : public PlayerClient {
  public:
      static PlayerClientSingleton* instance(void) {
          std::call_once(m_onceFlag, [] { m_instance = new PlayerClientSingleton; });
          return m_instance;
      }
  
      //Initialize is called from the .dll initialization (Dllmain)
      bool initialize(void) { 
          runThread();
      }
  
      void runThread(void) {
          m_thread = std::thread(&amp;PlayerClientSingleton::Run, this);
  
          if (m_thread.joinable()) {
              m_thread.detach();
          }
      }
  
      static PlayerClientSingleton* m_instance;
      static std::once_flag         m_onceFlag;
      std::thread                   m_thread;
  }
  
  class PlayerClient {
  protected:
      virtual void Run() {
          while (!m_threadEdn) {
              doSomething();
          }
      }
  }
  </code></pre>
  
  <p>Any idea why it does not work?</p>