#include #include #include using namespace std; void square(int x, int &sum) { sum += x * x; } int main() { int sum = 0; vector allthread; for (int i = 1; i <= 20; i++) { allthread.push_back(thread(&square, i, ref(sum))); // need extra ref(..) wrapper here due to multiple function calls } for (auto& th : allthread) { th.join(); } cout << "sum = " << sum << endl; return 0; }