I'm interested in (and confused) the details of constructing a std::thread object. According to cppreference , both the stream function and all arguments are copied to some stream storage, and then called.
1) What is the storage available for threads? Is it semantically equivalent to some streaming local storage, and are the variables destroyed when the stream function returns?
2) What is the category of argument values ββwhen passing a stream function? The cppreference description shows that they are passed as l-values ββ(they are given names anyway). My tests for GCC and clang seem to be the opposite, i.e. R-values. In particular, the following code does not compile:
void f(int& a) { std::cout << ++a << '\n'; } int main() { std::thread t(&f, 1); t.join(); return 0; }
It compiles if we change f to
void f(int&& a) { std::cout << ++a << '\n'; } int main() { std::thread t(&f, 1); t.join(); return 0; }
So what does the standard say about this?
c ++ multithreading language-lawyer c ++ 11 stdthread
Lingxi
source share