Art*_*tem 3 c++ multithreading boost stdstring boost-asio
我有以下功能:
void MyLib::sendMessage(const std::string& message) {
m_xIOService.post( boost::bind(&VoIPPhone::onSendMessage, this, message) );
}
void MyLib::onSendMessage(const std::string& message) {
m_xVoIPClient.sendMessage(message);
}
Run Code Online (Sandbox Code Playgroud)
所以我在一个线程中调用sendMessagein,并在主线程中调用onSendMessage.
问题是在这种情况下是否会通过boost复制消息字符串.如果没有 - 我怎样才能将字符串传递给onSendMessage函数并确保没有内存泄漏且消息字符串有效,而不是删除对象?
onSendMessage将在其中一个执行的线程中调用m_xIOService::run- 而不是在主线程中.
所有bind参数都被复制,因此message也将被复制.每当您想bind通过引用传递参数时,请使用boost::ref包装器.