std::ref
Use case
Pass references to threads (which copy arguments by default).
Explanation
std::thread copies its arguments by default. std::ref(x) creates a wrapper object that holds a pointer.
Code
#include <functional>
#include <iostream>
#include <thread>
void analyze(int &counter) { counter++; }
int main() {
int count = 0;
std::thread t(analyze, std::ref(count));
t.join();
std::cout << "Count: " << count << "\n";
return 0;
}
Output
$ ./src/c++11/build/std-ref
Count: 1