Created on 2015-07-15.00:00:00 last changed 81 months ago
Rationale (March, 2017):
Only automatic variables are captured. A lambda accessing a thread-local variable would be ill-formed.
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.
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.
History | |||
---|---|---|---|
Date | User | Action | Args |
2018-02-27 00:00:00 | admin | set | messages: + msg6003 |
2018-02-27 00:00:00 | admin | set | messages: + msg6002 |
2018-02-27 00:00:00 | admin | set | status: concurrency -> nad |
2015-07-15 00:00:00 | admin | create |