+-

我有以下功能:
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);
}
所以我在一个线程中调用sendMessagein,并在主线程中调用onSendMessage.
问题是在这种情况下是否会通过boost复制消息字符串.如果没有 – 我怎样才能将字符串传递给onSendMessage函数并确保没有内存泄漏且消息字符串有效,而不是删除对象?
最佳答案
onSendMessage将在执行m_xIOService :: run的一个线程中调用 – 而不是在主线程中.
将复制所有绑定参数,因此也将复制消息.每当您想通过引用传递绑定参数时,请使用boost :: ref wrapper.
点击查看更多相关文章
转载注明原文:c – 调用boost io_service.post的正确方法 - 乐贴网