gpt4 book ai didi

c++ - 如何在 nodejs 中获取多个 C++ 插件实例?

转载 作者:行者123 更新时间:2023-11-30 03:13:59 25 4
gpt4 key购买 nike

我想在 nodeJs 的多个实例中使用 C++ 库。我正在使用绑定(bind) npm 模块来包装 C++ 代码。但最终,C++ 库变成了共享对象。请帮我解决这个问题

我使用了带有新关键字的 require('bindings')('library_name')

在请求下一个实例之前我已经清除了请求缓存我使用了清除缓存的 npm 模块

for (var i = 0; i < config.data.length; i++) {
libraryObj[data[i]] = new require('library-name'); // c++ library wrapper
}

无论我做什么,C++ 库总是被共享,这不是预期的行为。

最佳答案

我建议您以不满足此要求的方式重新构建 C++ 插件。

你不能从同一个进程再次加载同一个模块,如果你这样做,它会返回相同的句柄(引用)

可以从其他进程加载

在 Windows 上

The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).

https://stackoverflow.com/a/9190995/12167785

在 Linux 上

If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it. The _init() routine, if present, is only called once. But a subsequent call with RTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY.

https://stackoverflow.com/a/9191147/12167785

关于c++ - 如何在 nodejs 中获取多个 C++ 插件实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58265779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com