Such a situation developed, for my C ++ application I redid the project of a console application written in C into a static library and everything works fine, but static and extern variables and functions are used there, so if I address the library methods twice in one session of the application (earlier it was not envisaged that int main main () will have several calls in 1 thread =)), then all static variables, of course, have already been declared and have values, which causes incorrect operation. Looking for a way to get rid of this behavior. The third-party code is quite extensive and correcting all the points in it will be a long and painstaking work. I suppose that in this case the appeal to the library methods in the new thread will help me, but I found in Google very few mention how to do this and that this is, in fact, a very dangerous shit. Maybe FreeLibrary will help me? Or is practice even easier?
Closed due to the fact that the question is too common for participants Vlad from Moscow , αλεχολυτ , Kromster , aleksandr barakin , Harry 29 Dec '16 at 20:52 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
2 answers
Believe me, it is better not to be lazy and fix now - remove all global variables. Yes, painstakingly, but correctly.
A pair of LoadLibrary / FreeLibrary will make this library work now, but forget about multi-threaded work at all
If it is a third-party library, especially a large one, it is dangerous to make structural transformation in it.
I see two solutions:
The first option :
- Create a structure containing fields for all static library variables.
Add methods (accepting such a structure as input):
but. "zeroing of static variables" (sequential setting of initial values by it)
b. "save the state of static variables" (full copying of the values of stat. variables into the structure)
at. "recovery of static variables from the structure" (full copying of values from the structure to the stat. variables)
- In the parent application, when needed, you can now save, reset and manage it.
Pros: there are no changes in logic, because not related to the main logic of the application. Full context management on the side of the parent.
Cons: enough hemorrhoids for ~ 1500 static variables.
The second option :
- Create a wrapper over a static library that accepts a set of commands by any means of interprocess communication, and the result of its work returns the same tools. And managed by the same means. It is hardly very laborious, in fact, it is enough to make an endless loop with listening to the channel (zeromq gives excellent means for network interaction, and also a boost license), understanding the commands to finish the work, perform such and such a function, wait for the command all the rest of the time.
- In the parent project, generate as many of these subprocesses as you need. They will hang, waiting for a death command, or a command to do something. Each process will have its own set of states of static variables.
I like the second option more, but it's up to you.
staticvariable. This will greatly simplify the understanding of your problem. - αλεχολυτ