Load multiple copies of a shared library

后端 未结 4 1757
栀梦
栀梦 2020-12-10 04:49

I am running Linux, and I would like to be able to make parallel function calls into a shared library (.so) which is unfortunately not threadsafe (I am guessing it has globa

4条回答
  •  青春惊慌失措
    2020-12-10 05:01

    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.

提交回复
热议问题