简体   繁体   English

Linux 中的 Singleton 与共享模块和 Static Z4789F23283B3A61F858B641A1BEF1 访问

[英]Singleton in Linux with Shared Modules and Static Memory Access

I have a process I am starting that is loading in multiple shared libraries.我有一个正在启动的进程正在加载到多个共享库中。 Some of the code in these shared libraries is the same (eg common files) and is using a singleton setup.这些共享库中的一些代码是相同的(例如公共文件),并且使用的是 singleton 设置。 eg例如

class MySingleton
{
  public:
    static MySingleton& GetInstance()
    {
      static MySingleton singleton;
      return singleton;
    }

    // ...
  private:
    // ...
}

I have run into a problem now where this MySingleton class is only being initialized once and being shared between the modules in memory (since they are being loaded into the same process).我现在遇到了一个问题,这个MySingleton class 只被初始化一次并在 memory 中的模块之间共享(因为它们被加载到同一个进程中)。 This seems to be a Linux specific problem, as I have had no issues with this on Mac OS or Windows where the application has been functioning fine.这似乎是 Linux 特定问题,因为我在 Mac OS 或 Windows 上没有遇到此问题,应用程序运行良好。

To further emphasize/clarify the question, I have the following code for instance:为了进一步强调/澄清问题,例如,我有以下代码:

// module 1
MySingleton::GetInstance().DoSomething(); // GetInstance() returns 0xABCDEFF
// module 2
MySingleton::GetInstance().DoSomethingElse(); // GetInstance() also returns 0xABCDEFF

My question is the following: Is there a way to force Linux/the shared modules to have independent static memory pools?我的问题如下:有没有办法强制 Linux/共享模块拥有独立的 static memory 池? Or am I at the mercy of the Linux operating system and how it decides to implement this?还是我受 Linux 操作系统的摆布以及它如何决定实现这一点?

In my case I am using this Singleton all over my code for a logging system...and it would be extremely inconvenient to go and have to modify this all over the place.在我的情况下,我在我的日志系统代码中使用这个 Singleton ......这对 go 来说非常不方便,并且必须在整个地方进行修改。 Thanks谢谢

After a few days (I know this question is only 30 minutes old, but I did finally find the solution)...I found what I was missing and doing on macOS but not on Linux.几天后(我知道这个问题只有 30 分钟,但我终于找到了解决方案)......我发现了我在 macOS 上缺少的东西,但在 Linux 上却没有。

I added the following extra compile flags to my common code and everything fell into place perfectly like it was on macOS -fPIC -fvisibility-inlines-hidden -fvisibility=hidden .我在我的通用代码中添加了以下额外的编译标志,一切都像在 macOS -fPIC -fvisibility-inlines-hidden -fvisibility=hidden上一样完美到位。

With these extra flags in place, the code is separate from each other and no longer overlapping in memory.有了这些额外的标志,代码就会彼此分离,并且在 memory 中不再重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM