将动态库多次加载到多个独立作用域中
我想将一个库多次动态加载到独立的作用域中,以便每个实例都有自己的内存。这可能吗?
我想不是以便携的方式。 POSIX/Unix/Linux 上的 dlopen
和朋友可以吗?或者至少我现在关心的是针对我的具体情况的 MacOSX(因此我以后可能在其他系统上也需要它)。
背景:我想要使用的库并非设计为多线程安全的。但是,如果每个线程仅使用该库的独立实例,它应该可以正常工作。
更多背景:它是 readline 库。在那里添加多线程支持基本上意味着重写整个事情。
I want to dynamically load a library multiple times into independent scopes, so that each instance has its own memory. Is that possible?
I guess not in a portable way. Is it possible with dlopen
and friends on POSIX/Unix/Linux? Or at least I care about MacOSX for my specific case right now (whereby I might need it later on other systems, too).
Background: The lib I want to use was not designed to be multithreading safe. However, it should work fine if each thread just uses an independent instance of the lib.
More background: It is the readline
lib. Adding multithreading support there basically would mean to rewrite the whole thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于你所说的“它自己的记忆”是什么意思。显然,对于 POSIX 线程,所有内存都是共享的,因此库的实例不能拥有“自己的内存”。
不过,您的意思可能是“以便每个实例都有自己的全局变量副本”,答案是肯定的:请参阅 dlmopen(3) 文档。您需要将
LM_ID_NEWLM
传递给它。注意:这仅适用于 Linux 和 Solaris,GDB 对加载到非默认链接器空间的库一无所知,因此调试问题目前非常困难。
Depends on what you mean by "its own memory". Obviously, with POSIX threads, all memory is shared, so an instance of the library can not have "its own memory".
What you probably meant though is "so that each instance has its own copy of global variables", to which the answer is yes: see dlmopen(3) docs. You'll want to pass
LM_ID_NEWLM
to it.Beware: this is Linux and Solaris only, and GDB doesn't know anything about libraries loaded into non-default linker space, so debugging problems is currently very hard.