如何在多线程环境中使用旧的单线程 C++ 库 [英] How to use an old single-threaded C++ library in a multithreaded environment

查看:53
本文介绍了如何在多线程环境中使用旧的单线程 C++ 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的 C++ 库,它是为在单线程环境中使用而设计的.

I have an old C++ library which has been designed for use in single-threaded environmens.

该库公开了用于初始化的接口,它改变了库的内部数据结构和用法,它只读取数据并进行计算.

The library exposes the interfaces for initialization, which change the internal data structures of the library, and usage, which only reads data and makes calculations.

我的目标是在 Windows 多线程应用程序中使用这个库,不同的线程调用使用不同数据初始化的 dll 实例.

My objective is to use this library in a Windows multithreaded application, with different threads calling instances of the dll initialized with different data.

假设重写dll以允许多线程会被禁止,有没有办法让一个DLL的多个实例存在于同一个进程中,具有单独的内存空间,或者通过其他方式获得类似的结果?

Assuming that rewriting the dll to allow multithreading would be prohibitive, is there some way to let multiple instances of a DLL exist in the same process, with separate memory spaces, or to obtain a similar result by other means?

推荐答案

如果 DLL 包含静态资源,那么这些资源将在所有创建的实例之间共享.

If the DLL contains static resources, then those would be shared among all instances created.

一种可能的方法是创建单个实例并使用某种锁定机制限制对其的访问.根据使用情况,这可能会降低性能,但如果不修改 DLL 的内部结构,则可能难以处理多个实例.

One possible way would be to create a single instance and restrict access to it using some kind of lock mechanism. This may reduce performance depending on usage, but without modifying internal structure of DLL, it may be difficult to work with multiple instance.

这篇关于如何在多线程环境中使用旧的单线程 C++ 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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