views:

603

answers:

3

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?

+1  A: 

This isn't the correct approach at all. By doing what you suggest, the variable, yes, is global to the library, and thus the program, but the data is private to the actual running process. You won't be able to share the values across running programs. @grieve is referring to a global accessed by multiple threads, but threads share the same parent process instance.

Across actual processes, you need to break out to an OS specific shared memory facility. Take a look at Shared Memory for details. It's a doable issue, but it's not particularly trivial to pull off. You'll also need a interprocess synchronization system like Semaphores as well to coordinate usage.

Will Hartung
There's no other approach in doing this specific problem other than using RPC, but that has caused a serious performance issue in the app I am developing. Also bear in mind that I do not own/control all the code that requires the linking, so I cannot modify it.
Edu Felipe
Shared Memory is NOT RPC. RPC is a remote procedure call with data serialized, Shared Memory is simply that -- memory shared between processes. Save for any synchronization issues, this is "full speed" native memory.
Will Hartung
Will, why do you say "the data is private to the .. process"? Edu has said that he's putting all his data in the shared memory segment. 'god' and all the objects it creates will be in shared memory, using a custom operator new() or whatever other memory allocation system he's using.
Tim Cooper
No, he said he was putting data in to a shared OBJECT. He wanted to have a single object, defined in a single file, and have that object "magically shared" across processes solely through using the linker. The linker can't and won't do that. Any "global" data in any library, even a "shared" library, is local to the process. You must use some OS based, out-of-process facility (such as shared memory) to achieve what he wants, and that won't come "for free" (though, apparently, the Boost library has utilities to more easily facilitate this process).
Will Hartung
A: 

I have feeling that god will be a server of some kind. Consider using a proper client/server architecture, so as to keep god away from the masses.

thAAAnos
+2  A: 

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

Chris Morley