在godbolt.org中编译代码时,我遇到以下错误:
In file included from <source>:5:
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/thread: In instantiation of 'std::thread::_State_impl<_Callable>::_State_impl(_Args&& ...) [with _Args = {void (Robot::*)(), Robot&}; _Callable = std::thread::_Invoker<std::tuple<void (Robot::*)(), Robot> >]':
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/thread:226:20: required from 'static std::thread::_State_ptr std::thread::_S_make_state(_Args&& ...) [with _Callable = std::thread::_Invoker<std::tuple<void (Robot::*)(), Robot> >; _Args = {void (Robot::*)(), Robot&}; std::thread::_State_ptr = std::unique_ptr<std::thread::_State>]'
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/thread:149:46: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Robot::*)(); _Args = {Robot&}; <template-parameter-1-3> = void]'
<source>:154:51: required from here
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/thread:211:46: error: could not convert '{std::forward<void (Robot::*)()>((* & __args#0)), std::forward<Robot&>((* & __args#1))}' from '<brace-enclosed initializer list>' to 'std::tuple<void (Robot::*)(), Robot>'
211 | : _M_func{{std::forward<_Args>(__args)...}}
| ^
| |
| <brace-enclosed initializer list>
Compiler returned: 1
看起来该错误是在使用std::thread创建线程时出现的。该守则的简化版本为:
class Robot {
private:
vector<double> position;
double area_covered;
mutex mtx;
public:
Robot(const vector<double> &initial_position) {
position = initial_position;
area_covered = 0.0;
}
void get_area() {
// ...
}
};
int main () {
vector<vector<double> > path{
{0.00359, -0.0013}, {0.00608, -0.00281}, {0.00756, -0.0027} };
Robot r3(path[0]);
std::thread thread1(&Robot::get_area, r3); // one thread
thread1.join();
}
问题是,像这样将r3
传递给线程构造函数会试图复制,而robot
是不可复制的,也是不可移动的(因为std::mutex
)。
可以通过指针传递对象:
std::thread thread1(&Robot::get_area, &r3);
或从
中使用std::reference_wrapper
:
std::thread thread1(&Robot::get_area, std::ref(r3));
联机查看