c++,dll 的多个实例,单例

发布于 2024-10-10 19:32:25 字数 177 浏览 6 评论 0原文

我有一个 DLL,其中定义了单例。

我有一个可以加载此 DLL 的多个实例的应用程序。

DLL 需要每个 DLL 实例有一个单例实例,否则会崩溃。

我发现多个 DLL 实例只有一个单例实例。为什么?我该如何解决它(如果可能的话,不将单例重构为其他东西)?

感谢您的任何帮助。

I have got a DLL in which a singleton is defined.

I have got an app which can load multiple instances of this DLL.

The DLL needs a singleton instance per DLL instance, otherwise it will crash.

I observed that there was only one singleton instance for multiple DLL instances. Why? How can I resolved it (if possible, without refactoring the singleton into something else)?

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-10-17 19:32:25

您提到您的应用中有多个实例,这意味着它们都位于同一进程中。

像任何其他静态成员一样,单例仅限于每个应用程序一个,无论它们是否属于从 DLL 等加载的对象。

You mentioned that you have multiple instances inside your app, which implies that they all live inside the same process.

Singletons like any other static member are limited to one per application regardless of whether they belong to an object loaded from a DLL etc.

夏末的微笑 2024-10-17 19:32:25

不重构代码就没有办法。 DLL被“加载”到进程空间中。其中定义的任何静态成员对于进程来说都是静态的(加载的 DLL 没有自己的内存)。

您必须编写一个非标准的“单例”才能获取多个对象。

No way without refactoring your code. A DLL is "loaded" into the process space. Any static member defined in there is static for the process (a loaded DLL doesn't have its own memory).

You'll have to write a non-standard "singleton" to get multiple objects.

掩耳倾听 2024-10-17 19:32:25

如果您没有 dll 的源代码,那么您必须在不同的进程中加载​​它,每个进程加载一个“单例”。这些可能是主进程的简单子进程,仅处理 dll 通信部分。

当然,您必须在主进程和子进程之间提供一些通信方案,这取决于您使用 dll 的程度。难道只是几次调用就产生大量数据吗?或者每次运行都有不同的许多不同的调用?

一般来说,如果您使用 dll 进行多个简单调用,那么重构您自己的代码可能会更容易。

And if you don't have the sources to the dll, then you must load it in different processes, one "singleton" per process. These could be simple child-processes to your main process that just handle the dll communication part.

Then of course, you must come with some communication scheme between your main process and your child processes, which will depend on how much you are using the dll. Is it just a couple of calls with a lot of data? Or a lot of different calls that differ from run to run?

Generally if you are using the dll to make more than a couple of simple calls it's probably easier to refactor your own code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文