This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2024-08-20
Consider the following example:
void f() { thread_local int n = 10; std::thread([&] { std::cout << n << std::endl; }).join(); }
This function prints 0, because:
The lambda does not capture n
n is not initialized on the spawned thread prior to the invocation of the lambda.
Additional note, March, 2016:
SG1 discussed this issue and concluded that the issues should be resolved follows:
If the program would result in a capture by reference of a local thread-local variable, then it is ill-formed.
If the program has a capture by value of a local thread-local variable, then a copy of the value from the calling thread is captured (and initialized in the calling thread, if necessary).
The rationale for #1 is that, if we allowed capture of local thread-locals, some programmers will have one intuition of what to expect and other programmers will have the opposite intuition. It's better to forbid both interpretations. We don't want to say simply that there is no capture by reference of thread-locals, because simply ignoring the local thread-local might result in name-lookup finding a global variable by the same name, which would be very confusing.
Rationale (March, 2017):
Only automatic variables are captured. A lambda accessing a thread-local variable would be ill-formed.