Let's start with a piece of code (Coliru):
#include <iostream> #include <thread> using namespace std; struct A { thread_local static A* p_a; thread_local static int i; }; thread_local int A::i; thread_local A* A::p_a; int main( ) { A::p_a = new A; auto lambda = [](A* a) { a->i = 1; // Prints 1 (below, of course) std::cout << a->i << std::endl; }; std::thread t(std::bind(lambda, A::p_a)); t.join(); // Prints 0 (it hasn't been modified) std::cout << A::p_a->i << std::endl; return 0; } As all of you can see, the second thread modifies its thread local copy of A::i, even though I accessed it from another thread local object of another thread. Is it the expected behaviour? Because it makes impossible to get info from another thread using a "referer" unless I pass a pointer or reference to the foreign's thread_local object which I want to read.
With "referer" I refer something which manages or can give you access to its own thread_local variable from its thread. But that's impossible!! Any expression yielding a thread_local variable, no matter from whom (I have done different test, even with accessor functions), ends by using the thread_local instance of the reading thread.
thread_localto communicate between threads is a in keeping with the spirit ofthread_localstorage. Why not just use a normalstatic?Apointer, you're not. The compiler sees it is astaticand ignores the pointer and goes straight for the single (per thread in this case) static instance. The wholethread_localbusiness is actually irrelevant to that.