C++共享对象初始化构造函数析构函数

C++ shared object initialization constructor destructor

我正在使用 g++ 编译的共享对象用作日志记录 API。我已经编写了一个小实用程序来运行该库,但我发现每次程序启动和退出时都会调用共享对象 constructor/destructor。我希望能够保持 .so 文件中任何 variables/objects 的状态,即使没有活动的应用程序链接。是否有实现此目的的机制?

I'd like to be able to maintain the state of any variables/objects in the .so file, even if there are no active applications linked. Is there a mechanism for accomplishing this?

在现代OS中,内存中的一切“属于”一个或多个进程,没有“.so 没有有效的应用程序。

此外,当您的 .so 链接到多个活动的应用程序(运行 进程)时,仍然 没有您的库可以共享的状态记录 -- 您的 .so 的每个实例将无法观察到 .so 的其他实例在其他进程中处于活动状态,除非使用某些 IPC 机制。

可以使用shmat, and record shared state there. That state would also persist until explicitly removed via smtctl系统调用创建共享内存段。

注意:以这种方式使用共享内存充满了复杂性,并且对于日志记录 API 库来说很可能是矫枉过正。