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 in开发者_运维百科stance of the lib.
More background: It is the readline
lib. Adding multithreading support there basically would mean to rewrite the whole thing.
so that each instance has its own memory.
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.
精彩评论