Integrating legacy C code in multi-threaded C++ code -


assume have legacy c file functions solve linear equations , several corresponding global variables.

lineq.c:

/* macro definitions */ ... /* global vars */ ... /* functions make use of above variables */ void solvelineq(...); 

now want use legacy library in modern multi-threaded c++ application. therefore, want write kind of wrapper class lineqsolver provides oo interface solving linear equations , internally calls functions of our legacy c library.

however, should possible there multiple instances of lineqsolver used different threads. requires each instance/each thread has own copies of global variables in lineq.c. how can achieved, if don't want modify lineq.c?

a possible solution can imagine copy global variables , functions c file class lineqsolver, making them data , function members. then, each instance of lineqsolver operate on private copy of former globale variables. however, copy-paste programming style rather bad, when there update lineq.c , need copy-paste changes our code again.

what other possibilities have actual code solve linear equations stays in lineq.c , called lineqsolver?

you can use thread_local keyword c++11. work if function always initializes needed static variables in beginning.

if have more complex picture - more work needed. example:

int myvar1, myvar2;  void initstaticvars() {    .... }  void solvelineq(...); 

if add thread_local specifier variables above , call initialization function @ beginning of program, initialize these variables calling thread. on other threads initial values zeroes.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -