您不能将一个引用(或多个const引用)直接传递给线程,因为std::thread它将复制/移动它们。而是使用std::reference_wrapper:
void foo(int& b) { b = 10; } int a = 1; std::thread thread{ foo, std::ref(a) }; //现在真正通过了“ a”作为参考 thread.join(); std::cout << a << '\n'; //输出10
void bar(const ComplexObject& co) { co.doCalculations(); } ComplexObject object; std::thread thread{ bar, std::cref(object) }; //“对象”作为const&传递amp; thread.join(); std::cout << object.getResult() << '\n'; //输出结果