Reputation: 1852
I have some old C static lib which have code similar to:
.h file
void setValue(int);
.c file
int value;
void setValue(int i) { value = i; }
and from main app (written with c++) it's just invoked by calling (there's extern C include in header file of course)
setValue(42);
In single thread everything works just fine and of course if use same lib in 2 different threads "value" is shared between them.
What would be best way to use separate memory for that lib for every thread?
so if there would be 2 threads T1 and T2, it would work like:
T1.setValue(1);
T2.setValue(2);
T1.start();
T2.start();
// T1 works with value "1"
// T2 works with value "2"
Upvotes: 1
Views: 93
Reputation: 18228
Assuming you can change and recompile the code, you can mark the value
static variable with either __thread
or with thread_local
(C++11) or with __declspec(thread)
(MSVC). Then each thread will have a separate storage for the variable.
Upvotes: 1