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.
如果我在每个函数的开头执行锁定同步,并考虑到上帝本身可以新建/ malloc其他东西,但这些东西永远不会自己返回给调用者(上帝mallocs仅供内部使用),最简单的是什么如果可能的话,这样做的方式。
How can that be extended to an arbitrary number of programs linked to this shared library?
如何将其扩展到链接到此共享库的任意数量的程序?
3 个解决方案
#1
2
Boost Interprocess library has high(er) level, portable shared memory objects.
Boost Interprocess库具有高(er)级别的可移植共享内存对象。
#2
1
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.
这根本不是正确的方法。通过执行您的建议,变量yes是库的全局变量,因此是程序的全局变量,但数据对于实际运行的进程是私有的。您将无法在正在运行的程序*享值。 @grieve指的是由多个线程访问的全局,但线程共享相同的父流程实例。
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.
在实际流程中,您需要突破到特定于操作系统的共享内存设施。有关详细信息,请查看共享内存。这是一个可行的问题,但这并不是特别容易实现的。您还需要一个像Semaphores这样的进程间同步系统来协调使用。
#3
0
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.
我觉得上帝会成为某种服务器。考虑使用适当的客户端/服务器架构,以使上帝远离群众。
#1
2
Boost Interprocess library has high(er) level, portable shared memory objects.
Boost Interprocess库具有高(er)级别的可移植共享内存对象。
#2
1
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.
这根本不是正确的方法。通过执行您的建议,变量yes是库的全局变量,因此是程序的全局变量,但数据对于实际运行的进程是私有的。您将无法在正在运行的程序*享值。 @grieve指的是由多个线程访问的全局,但线程共享相同的父流程实例。
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.
在实际流程中,您需要突破到特定于操作系统的共享内存设施。有关详细信息,请查看共享内存。这是一个可行的问题,但这并不是特别容易实现的。您还需要一个像Semaphores这样的进程间同步系统来协调使用。
#3
0
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.
我觉得上帝会成为某种服务器。考虑使用适当的客户端/服务器架构,以使上帝远离群众。