S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Linux下C++ hot reload

Posted on 2011-07-01 17:03 S.l.e!ep.¢% 阅读(1313) 评论(0)  编辑 收藏 引用 所属分类: Unix

Linux下C++热加载实例

Friday, 3. June 2011 - 16:21 |1 comment »

假设一种环境,我们要对服务热拔插一个动态库(.so文件),所要考虑的是多线程环境的兼容,不会因为动态库替换后造成栈损毁而崩溃
这边想到的方法就是封装一个dlopen过程作为对象实例加载(见load_so.h),当发出更新动态库时重新dlopen过程,替换原先的实例,注意这个替换过程必须是温和的、无逢的,这边我们使用智能指针实现。

具体更新的实现通过一个单例(见do_sth.h),调用Reload重新加载动态库。

我们构造一个极简单的动态库测试:
make_so.h

1
2
3
4
5
6
7
8
#include "say.h"
extern "C"
{
    void Enter(const std::string&str)
    {
        Say::instance().Sth(str);   //在这里动态库又过来调用了主程序的单件
    }
}

say.h 打印消息,这边只是声明一个单例,具体实现于主程序当中

1
2
3
4
5
6
#include "singleton.h"
class Say :public Singleton <Say>
{
    public:
        void Sth(const std::string&str);
};

通过编译生test.so:
g++ make_so.cpp -fPIC -shared -pthread -rdynamic -lboost_thread -lboost_system -o test.so -L[boost库目录]

主程序 test.cpp ,用来测试这个动态库test.so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <boost/thread.hpp>
#include "load_so.h"
#include "say.h"

class DoSth :public Singleton
{
    public:
        DoSth():m_so(new LoadSo("./test.so"))
        {
        }

        DynamicSo_ptr Get()
        {
            boost::mutex::scoped_lock lock(m_mtx);
            return m_so;
        }

        void Reload()
        {
            boost::mutex::scoped_lock lock(m_mtx);
            m_so.reset(new LoadSo("./test.so"));
        }
    private:
        DynamicSo_ptr m_so;
        boost::mutex m_mtx;
};



/// Say
void Say::Sth(const std::string&str)
{
    std::cout<< str << std::endl;
}




////////////////////////// 测试代码 //////////////////////////////////////

//更新动态库
void test()
{
    for(int i=0;i<100;++i)
    {
        sleep(1);
        DoSth::instance().Reload();
    }
}

//运行动态库
void test2()
{
    for(int i=0;irun("12");
    }
}

int main()
{
    std::cout<<"run\n";

    boost::thread thread1(&test2);
    boost::thread thread2(&test);

    thread1.join();
    thread2.join();

    return0;
}

编译主程序:

g++ -Wall test2.cpp -o test2 -pipe -pthread -ldl -Wl,–export-dynamic -lboost_system -lboost_thread -L[boost库目录]

这边一定要加“-Wl,–export-dynamic”以便导出主程序的符号供动态库回调。

编译成功后生成 test 可执行文件,运行:

#./test
run
12
12
12
12
….

假设这时我们修改 make_so.h

1
2
3
4
5
6
7
8
#include "say.h"
extern "C"
{
    void Enter(const std::string&str)
    {
        Say::instance().Sth(str +",ab");   //修改输出
    }
}

g++ make_so.cpp -fPIC -shared -pthread -rdynamic -lboost_thread -lboost_system -o test.so -L[boost库目录]
重新编译后,这时我们主程序会马上响应,输出:

12,ab
12,ab
12,ab
12,ab
….

说明热替换是成功的。

在实际运用中,当要替换动态库时可以在程序中使用
DoSth::instance().Reload();
具体方法很多,可以通过socket、中断信号、监听文件系统、定时更新等方式。

load_so.h 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef LOAD_SO_H
#define LOAD_SO_H

#include
#include
#include
using namespace std;

class LoadSo
{
    typedefvoid(*Func)(const std::string&suid);
    public:
        LoadSo(constchar* so_file)
        {
            load(so_file);
        }

        ~LoadSo()
        {
            Close();
        }

        void Close()
        {
            dlclose(m_handle);
        }

        void run(const std::string&str )
        {
            m_func(str);
        }
    private:
        bool load(constchar* so_file)
        {
            m_handle = dlopen(so_file, RTLD_LAZY);
            if(!m_handle){
                std::string error("Cannot open library: ");
                throw std::runtime_error(error + dlerror());
                returnfalse;
            }

            dlerror();
            m_func =(Func) dlsym(m_handle, "Enter");
            constchar*dlsym_error = dlerror();
            if(dlsym_error){
                dlclose(m_handle);
                std::string error("Cannot load symbol: ");
                throw std::runtime_error(error + dlsym_error);
                returnfalse;
            }
            returntrue;
        }

        Func m_func;
        void* m_handle;
};
typedef boost::shared_ptr DynamicSo_ptr;

#endif

说在最后,我在一些测试中发现有时并不自动切换到新的动态库的情况,另一个做法就是备用两个动态库,那么在Reload时在两个so文件之间切换,这样可以确保更新,额外给自己找的好处是可以保留旧版本的so。


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理