I have a variable in file tracker.hpp:
namespace TRIALS
{
static thread_local int a = -1;
}
I have another class in file called EMP in ema.hpp/ema.cpp
namespace Algo
{
class EMP
{
public:
void Sample();
};
}
namespace Algo
{
void EMP::Sample()
{
std::cout << "model " << std::this_thread::get_id() << " " << &TRIALS::a << " " << TRIALS::a << std::endl;
}
}
Then my main file I have
auto model = Algo::EMP();
void Simulate(const int a)
{
TRIALS::a = a;
model.Sample()
std::cout << "worker " << std::this_thread::get_id() << " " << &TRIALS::a << " " << TRIALS::a << std::endl;
}
int main()
{
std::cout << &TRIALS::a << std::endl;
const int nthreads = 1;
std::cout << "main " << std::this_thread::get_id() << " " << &TRIALS::a << " " << TRIALS::a << std::endl;
std::vector<std::thread> threads;
for(int i=0; i<nthreads; ++i)
{
threads.emplace_back(&Simulate, i);
}
for(auto &thread : threads)
{
thread.join();
}
std::cout << "main " << std::this_thread::get_id() << " " << &TRIALS::a << " " << TRIALS::a << std::endl;
return 0;
}
I am just running one thread for debugging but this is the output:
0x7f9540b621d8
main 140279012532800 0x7f9540b621d8 -1 (As expected)
model 140278985606912 0x7f953f1b469c -1 (Shouldn't this be 0??)
worker 140278985606912 0x7f953f1b4698 0 (As expected)
main 140279012532800 0x7f9540b621d8 -1 (As expected)
I was under the impression that each thread has it's own local copy of TRIALS::a. The a in model correctly gets incremented but when it returns from the function in the same thread, the value is still 0. I am printing out the thread ids and the address of a for good measure and I am seeing that there are actually 3 different versions of TRIALS::a despite only two total threads.
As a bonus question, what is the difference between static thread_local int a
and thread_local int a
?