提升线程:使用已删除的功能错误

时间:2014-03-12 14:41:35

标签: c++ linux multithreading boost b2

我正在尝试使用boost中的多线程来处理多个请求,并且在接收到特定消息后,我将创建一个新线程来处理它,如下所示:

的main.cpp

/** * Destination Machine Request Handler. * * @param msg Received message. * @param ec Error code. */ void handover_request_handler(odtone::mih::message &msg, const boost::system::error_code &ec) { if (ec) { log_(0, FUNCTION, " error: ", ec.message()); return; } // Do some stuff }

void event_handler(odtone::mih::message &msg, const boost::system::error_code &ec) { if (ec) { log_(0, FUNCTION, " error: ", ec.message()); return; }

switch (msg.mid()) { // Destination Cloud Server received HO Commit Message case odtone::mih::indication::n2n_ho_commit: { boost::thread thrd(boost::bind(&handover_request_handler, msg, ec)); thrd.join(); } break; }

}

当我尝试使用b2工具编译它时,我收到以下错误:

gcc.compile.c ++ ../../bin.v2/app/lte_mih_usr/gcc-4.6/debug/link-static/runtime-link-static/main.o main.cpp:在函数'void event_handler(odtone :: mih :: message&,const boost :: system :: error_code&)'中: main.cpp:189:69:错误:使用已删除的函数'odtone :: mih :: message :: message(const odtone :: mih :: message&)' 在../../inc/odtone/mih/request.hpp:24:0中包含的文件中,                  来自main.cpp:11:

那么如何解决这个问题呢?

非常感谢。

1 个答案:

答案 0 :(得分:1)

thread构造函数复制其参数,message类型不可复制。要传递对目标函数的引用,您需要使用boost::ref(msg)

另请注意,bindthread一起使用是不必要的:

boost::thread thrd(&handover_request_handler, boost::ref(msg), boost::ref(ec));

thread构造函数实现与bind相同的语义,因此使用bind只会添加不必要的额外复制。

相关问题