Load multiple copies of a shared library

末鹿安然 提交于 2019-11-28 09:58:02
Employed Russian

You can load multiple independent copies of the library like this:

#define _GNU_SOURCE
#include <dlfcn.h>
...
void *handle = dlmopen(LM_ID_NEWLM, "/path/to/library.so", RTLD_NOW);

More info here.

Instead of using threads, you can use multiple processes, each doing some of the work. This is very common on *nix, and is usually easier to code.

Looks like a bad idea. That is no more possible with shared libraries as it would be with static ones.

You could probably use dlopen() with RTLD_LOCAL flag so that subsequent calls to dlopen won't see it's allready loaded and make it work as you want... but it still looks like a bad design idea. If you have performance issues it would be better avoiding to clutter memory with several copies of the same library.

I would suggest either using several processes or going the mutex way, it's probably more efficient.

As you work on Linux there also may exists other approaches if you can access the source code of the library, like renaming it's symbols to have as many separate instances as needed... Well, once you get the source there may be other ways, like making the library thread safe.

What library is it? Is it something large? I'm wondering if you couldn't fix the library to be threadsafe in some way, then build your code using the threadsafe version of the library. It depends on the size of the library, and what's wrong with it, but if you could fix the library, you'd be able to build your app the way you want, as well as help everyone else out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!