The std::thread
constructor copies the supplied values, without converting to the expected argument type (which is reference type in this case, seeupdate()
). So we need to wrap the arguments that really needs to be references in std::ref
.
So, in std::thread the std::ref in to make in compile with correct augument type.
Also, an example where std::ref is useful
std::vector<int> v1, v2; void test() { for (std::vector<int>& vv : // Compiles { std::ref(v1), std::ref(v2) } // Compiler rejects this with: // binding reference of type 'vector<...>' to value of // type 'const vector<...>' drops 'const' qualifier // { v1, v2} ) { vv.push_back(3); }