代码之家  ›  专栏  ›  技术社区  ›  PiotrK

函数内部的静态变量和动态链接

  •  0
  • PiotrK  · 技术社区  · 7 年前

    例如,如果在标头中内联了以下单例构造:

    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()
            {
    
            }    
    }
    

    包含在多个共享库(DLL/SO)中,可执行文件动态链接(使用 LoadLibrary / dlopen )我能指望每个模块(至少在Windows、Linux和osx上)只有一个单例实例吗?

    例如:

    • file1.dll 代码包含一个单例实例,
    • file2.dll 代码包含另一个实例
    • file3.exe

    如果 文件3.exe 调用函数(通过 GetProcAddress 文件1.dll ,我能指望访问吗 文件1.dll 函数体中的单例实例?

    0 回复  |  直到 7 年前