English 中文(简体)
Load multiple copies of a shared library
原标题:

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 global datastructures).

For performance reasons, I do not want to simply wrap the function calls in a mutex.

What I would like to do is to spawn, say 4 threads, and also load 4 copies of the same library into the process memory. Each thread then makes the function calls into its own copy of the library.

Unfortunately, dlopen does not allow me to load more one instance of any library.

Does anyone know of any method which will allow me to load the library more than once? (Other than making 4 copies of the .so file, each with a different name)

问题回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签