加载共享库的多个副本 [英] Load multiple copies of a shared library

查看:180
本文介绍了加载共享库的多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行Linux,我想能够使并行函数调用到一个共享库(的.so),这是不幸的是没有线程(我猜测它具有全局数据结构)。

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.

我想这样做是为了产卵,说4个线程,并且还加载同一个库的4份到进程的内存。每个线程则使函数调用到自己的库副本。

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.

不幸的是,的dlopen不允许我加载任何库更多的一个实例。

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

有谁知道这会让我加载库不止一次任何方法吗? (除制作.so文件4份,每一个不同的名称)

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);

更多信息这里

这篇关于加载共享库的多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆