Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an old single-threaded C++ library in a multithreaded environment

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.

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

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?

like image 608
Coffee on Mars Avatar asked Dec 16 '22 17:12

Coffee on Mars


1 Answers

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

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.

like image 132
Shamim Hafiz - MSFT Avatar answered May 18 '23 13:05

Shamim Hafiz - MSFT