Static variables inside function and dynamic linkage

248 Views Asked by At

If I have, for example, following singleton construction inlined in headers:

class singleton_t
{
    public:
        static std::shared_ptr<singleton_t> instance()
        {
            static std::shared_ptr<singleton_t>
                                 instance{new singleton_t};
            return instance;
        }
    private:
        singleton_t()
        {

        }    
}

That are included in several Shared libraries (DLL/SO) and Executables linked dynamically (using LoadLibrary / dlopen) can I count on to have exactly one singleton instance per module (at least on Windows, Linux, and OS X)?

So, for example:

  • file1.dll code contains one singleton instance,
  • file2.dll code contains another instance
  • file3.exe contains another instance

If file3.exe calls function (via GetProcAddress) from file1.dll, can I count on accessing file1.dll's singleton instance in that function body?

I know C++ standard does not say anything about dynamic linkage, I am looking for platform-specific knowlege. The linkage will be only dynamic.

0

There are 0 best solutions below