用mn查看单例模式符号表——动态加载的动态链接库中的单例

nm - display name list(symbol table)

问题

有一个疑问由来已久,单例分单线程安全,多线程安全。那么如果动态加载链接库,单例还能保证只存在唯一实例吗?
于是,打算写代码验证一下

验证

1. 代码准备

总共有三个文件singleton.h, operation.cpp, main.cpp,内容如下

singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H

class Singleton{
public:
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
    static Singleton* instance() {
        static Singleton s;
        return &s;
    }
    int count;
private:
    Singleton(): count(0) {};
};

#endif
operation.cpp
#include "singleton.h"

#include <iostream>

extern "C" void operation() {
    std::cout << "In dll count(d1): " << (Singleton::instance()->count) << std::endl;

    ++ Singleton::instance()->count;

    std::cout << "In dll after ++ count(d2) : "
            << (Singleton::instance()->count) << std::endl;
}
main.cpp
#include "singleton.h"

#include <iostream>
#include <dlfcn.h>

int main() {
    Singleton::instance()->count = 100;    
    std::cout << "In main count(m1): " << Singleton::instance()->count << std::endl;
 
    void* handle = dlopen("./operation.so", RTLD_LAZY);
    typedef void (*operationFunc)();
    operationFunc hello = (operationFunc) dlsym(handle, "operation");

    hello();
    std::cout << "In main count(m2): " << Singleton::instance()->count << std::endl;
    dlclose(handle);
}

2. 编译运行

在程序执行前可以先分析一下count(d1), count(d2), count(m1), count(m2)会分别是多少呢?
我们简单写一个Makfile方便编译执行

main: main.cpp operation.so
	$(CXX) $(CXXFLAGS) -o main main.cpp -ldl
operation.so: operation.cpp
	$(CXX) $(CXXFLAGS) -shared -fPIC -o operation.so operation.cpp
clean:
	rm -f main operation.so
.PHONY: clean

编译执行结果如下

$ make
g++  -shared -fPIC -o operation.so operation.cpp
g++  -o main main.cpp -ldl

$ ./main
In main count(m1): 100
In dll count(d1): 0
In dll after ++ count(d2) : 1
In main count(m2): 100

3. 结果分析

很明显,count(d1)=0; count(d2)=1; count(m1)=100; count(m2)=100;
这说明main函数中的Singleton::instance()->count = 100; 仅对count(m1),count(m2)有效;operation中的++ Singleton::instance()->count;仅对count(d1), count(d2)有影响。即main中和动态库(operation)中,分别拥有自己实例,单例没有保证实例的唯一性

我们接着进一步分析:
用nm查看main的符号表和动态符号表(-D)

$ nm -C main | grep Singleton
0000000000012028 u guard variable for Singleton::instance()::s
0000000000000d5c W Singleton::instance()
0000000000000dd4 W Singleton::Singleton()
0000000000000dd4 W Singleton::Singleton()
0000000000012020 u Singleton::instance()::s

$ nm -C -D main | grep Singleton
$

动态符号表中没有与Singleton相关的符号。Singleton::instance()在静态符号表中存在,但在动态表中不存在,这也就是说,动态连接器(dynamic linker)在加载operation.so的时候,无法找到属于动态库的Singleton::instance()实例,只能再构造一个新的实例。动态库的实例在动态库中是唯一的,但是不能保证在main和operation.so中唯一。

所以,解决问题的关键在于如何让动态库暴露符号表,让动态链接器链接的时候可以检测到。答案就是链接的时候加上参数-rdynamic

main: main.cpp operation.so
	$(CXX) $(CXXFLAGS) -o main main.cpp -ldl -rdynamic
operation.so: operation.cpp
	$(CXX) $(CXXFLAGS) -shared -fPIC -o operation.so operation.cpp
clean:
	rm -f main operation.so
.PHONY: clean

修改后再编译执行

$ make
g++  -shared -fPIC -o operation.so operation.cpp
g++  -o main main.cpp -ldl -rdynamic

$ ./main 
In main count(m1): 100
In dll count(d1): 100
In dll after ++ count(d2) : 101
In main count(m2): 101

此时,单例便能保证实例的唯一性
我们再来查看符号表

$ nm -C main | grep Singleton
0000000000012028 u guard variable for Singleton::instance()::s
00000000000010bc W Singleton::instance()
0000000000001134 W Singleton::Singleton()
0000000000001134 W Singleton::Singleton()
0000000000012020 u Singleton::instance()::s

$ nm -C -D main | grep Singleton
0000000000012028 u guard variable for Singleton::instance()::s
00000000000010bc W Singleton::instance()
0000000000001134 W Singleton::Singleton()
0000000000001134 W Singleton::Singleton()
0000000000012020 u Singleton::instance()::s

可以发现动态符号表也把所有的符号暴露出来了

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值