How to use the old single-threaded C ++ library in a multi-threaded environment

I have an old C ++ library that was developed for use in single-threaded environments.

The library provides initialization interfaces that modify the internal data structures of the library and use that only reads data and performs calculations.

My goal is to use this library in a multi-threaded Windows application, with different threads invoking DLL instances initialized with different data.

Assuming rewriting the dll to allow multithreading would be prohibitive, is there a way to allow multiple DLL instances in the same process with separate memory cells, or get a similar result in other ways?

+4
source share
2 answers

If the DLL contains static resources, then they will be shared for all created instances.

One possible way is to create a single instance and restrict access to it using some kind of locking mechanism. This may decrease performance depending on use, but without changing the internal structure of the DLL, it can be difficult to work with multiple instances.

+6
source

Sharing static resources between all threads connected to the same DLL in the process is up to you here.

However, there is a trick for this. As long as DLLs have different names, the system considers them different, so separate instances of code and data are created.

To achieve this, for each thread, copy the DLL to a temporary file and load it using LoadLibrary . You should use explicit binding ( GetProcAddress ), not lib files, but this is really the only way.

+5
source

Source: https://habr.com/ru/post/1340157/


All Articles