title: Multiple instances of singleton across shared libraries on Linux
body: <p>My question, as the title mentioned, is obvious, and I describe the scenario in details.
  There is a class named singleton implemented by singleton pattern as following, in file singleton.h:</p>
  
  <pre><code>/*
   * singleton.h
   *
   *  Created on: 2011-12-24
   *      Author: bourneli
   */
  
  #ifndef SINGLETON_H_
  #define SINGLETON_H_
  
  class singleton
  {
  private:
      singleton() {num = -1;}
      static singleton* pInstance;
  public:
      static singleton&amp; instance()
      {
          if (NULL == pInstance)
          {
              pInstance = new singleton();
          }
          return *pInstance;
      }
  public:
      int num;
  };
  
  singleton* singleton::pInstance = NULL;
  
  #endif /* SINGLETON_H_ */
  </code></pre>
  
  <p>then, there is a plugin called hello.cpp as following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "singleton.h"
  
  extern "C" void hello() {
      std::cout &lt;&lt; "singleton.num in hello.so : " &lt;&lt; singleton::instance().num &lt;&lt; std::endl;
      ++singleton::instance().num;
      std::cout &lt;&lt; "singleton.num in hello.so after ++ : " &lt;&lt; singleton::instance().num &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>you can see that the plugin call the singleton and change the attribute num in the singleton.</p>
  
  <p>last, there is a main function use the singleton and the plugin as following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;dlfcn.h&gt;
  #include "singleton.h"
  
  int main() {
      using std::cout;
      using std::cerr;
      using std::endl;
  
      singleton::instance().num = 100; // call singleton
      cout &lt;&lt; "singleton.num in main : " &lt;&lt; singleton::instance().num &lt;&lt; endl;// call singleton
  
      // open the library
      void* handle = dlopen("./hello.so", RTLD_LAZY);
  
      if (!handle) {
          cerr &lt;&lt; "Cannot open library: " &lt;&lt; dlerror() &lt;&lt; '\n';
          return 1;
      }
  
      // load the symbol
      typedef void (*hello_t)();
  
      // reset errors
      dlerror();
      hello_t hello = (hello_t) dlsym(handle, "hello");
      const char *dlsym_error = dlerror();
      if (dlsym_error) {
          cerr &lt;&lt; "Cannot load symbol 'hello': " &lt;&lt; dlerror() &lt;&lt; '\n';
          dlclose(handle);
          return 1;
      }
  
      hello(); // call plugin function hello
  
      cout &lt;&lt; "singleton.num in main : " &lt;&lt; singleton::instance().num &lt;&lt; endl;// call singleton
      dlclose(handle);
  }
  </code></pre>
  
  <p>and the makefile is following:</p>
  
  <pre><code>example1: main.cpp hello.so
      $(CXX) $(CXXFLAGS)  -o example1 main.cpp -ldl
  
  hello.so: hello.cpp
      $(CXX) $(CXXFLAGS)  -shared -o hello.so hello.cpp
  
  clean:
      rm -f example1 hello.so
  
  .PHONY: clean
  </code></pre>
  
  <p>so, what is the output?
  I thought there is following:</p>
  
  <pre><code>singleton.num in main : 100
  singleton.num in hello.so : 100
  singleton.num in hello.so after ++ : 101
  singleton.num in main : 101
  </code></pre>
  
  <p>however, the actual output is following:</p>
  
  <pre><code>singleton.num in main : 100
  singleton.num in hello.so : -1
  singleton.num in hello.so after ++ : 0
  singleton.num in main : 100
  </code></pre>
  
  <p>It proves that there are two instances of the singleton class.</p>
  
  <p>Why?</p>