我要移植到 android 平台的本机 C++ 项目基于几个静态和动态库,使用基于 c++11 的线程安全单例,这些单例在这些模块之间共享。
这是我的实际结构的示例:
brCore (Core elements, Manager (Singletons), Logger (Singleton) ect.) STATIC
brGrapics (Generic graphics) uses brCore STATIC
brOpenGLES (GLES renderer) uses brCore, brGraphics DYNAMIC
我的 Android 应用程序(也是 DYNAMIC)使用这三个库并在启动时动态加载 brOpenGLES。在 Linux 和 Windows 平台上一切正常,但在 android 上,我遇到了单例问题:
04-08 06:47:24.601: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.611: I/NativeActivitySimpleExample(1059): Singleton constructor invoked : brFileHandler
04-08 06:47:24.611: I/NativeActivitySimpleExample(1059): brFileHandler set internal data path: /data/data/com.binrev/files
04-08 06:47:24.611: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.031: I/NativeActivitySimpleExample(1059): Try to initialize Render API
04-08 06:47:24.031: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.051: I/NativeActivitySimpleExample(1059): Singleton constructor invoked : brRessourceManager
04-08 06:47:24.051: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.251: I/NativeActivitySimpleExample(1059): Try to init Logger: brCore
04-08 06:47:24.251: I/NativeActivitySimpleExample(1059): Search for existing logger
04-08 06:47:24.282: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.282: I/NativeActivitySimpleExample(1059): Singleton constructor invoked : brRessourceManager
04-08 06:47:24.282: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.282: I/NativeActivitySimpleExample(1059): Singleton constructor invoked : brConfigManager
04-08 06:47:24.601: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
04-08 06:47:24.611: I/NativeActivitySimpleExample(1059): Singleton constructor invoked : brFileHandler
04-08 06:47:24.611: I/NativeActivitySimpleExample(1059): brSingleton getInstance called
如您所见,线程安全单例的构造函数被多次调用,不应该被调用。在 Singleton(brFileHandler 内部数据路径)中设置的值在以后会丢失。看起来图书馆有单独的单例实例,无法在图书馆边界之间共享内容......
我该如何解决这个问题?谢谢你的帮助。