在C ++中的共享库(so)中的共享内存中创建对象 [英] Creating an object in shared memory inside a Shared Lib (so) in C++

查看:280
本文介绍了在C ++中的共享库(so)中的共享内存中创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在链接到此代码的每个人之间共享一个神"实例,并将其放置在共享对象中?

Is it possible to share a single 'god' instance among everyone that links to this code, to be placed in a shared object?

god* _god = NULL;
extern "C" 
{

int set_log_level(int level)
{
    if(_god == NULL) return -1;
    _stb->log_level(level);
    return 0;
}

int god_init(){
    if(_god == NULL){
        _god = new god(); //Magic happens here
    }
}

}

假设我在每个函数的开头执行锁同步,并考虑到神本身可以对其他事物进行新的/malloc分配,但是这些东西将永远不会返回给调用者(神mallocs仅供内部使用),如果可能的话,这是最简单的方法.

Provided that I perform a lock synchronization at the beginning of every function, and considering that God itself can new/malloc other things, but those things will never be returned themselves to the caller (God mallocs only for internal use), what is the simplest way of doing this, if possible.

如何将其扩展到链接到该共享库的任意数量的程序?

How can that be extended to an arbitrary number of programs linked to this shared library?

推荐答案

Boost Interprocess library has high(er) level, portable shared memory objects.

这篇关于在C ++中的共享库(so)中的共享内存中创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆