其他分享
首页 > 其他分享> > c – 如何通过boost :: interprocess :: message队列传递复杂对象(std :: string)

c – 如何通过boost :: interprocess :: message队列传递复杂对象(std :: string)

作者:互联网

有没有人有一些示例代码显示序列化std :: string的管道,通过boost :: interprocess :: message_queue发送它并再次将其取回?

解决方法:

您需要序列化数据,因为boost :: interprocess :: message_queue在字节数组上运行.如果您的所有消息都是字符串,那么只需:

size_t const max_msg_size = 0x100;
boost::interprocess::message_queue q(..., max_msg_size);

// sending
std::string s(...);
q.send(s.data(), s.size(), 0);

// receiving
std::string s;
s.resize(max_msg_size);
size_t msg_size;
unsigned msg_prio;
q.receive(&s[0], s.size(), msg_size, msg_prio);
s.resize(msg_size);

标签:c,boost,message-queue
来源: https://codeday.me/bug/20190723/1518181.html