Ad

Creating An Object In Shared Memory Inside A Shared Lib (so) In C++

- 1 answer

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
    }
}

}

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?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad