C++ 如何通过boost::进程间::消息队列传递复杂对象(std::string)
是否有人有一些示例代码显示序列化std::字符串的管道,通过boost::interprocess::message_队列将其发送并再次将其取出?您需要序列化数据,因为boost::interprocess::message_队列在字节数组上运行。如果所有消息都是字符串,只需执行以下操作:C++ 如何通过boost::进程间::消息队列传递复杂对象(std::string),c++,boost,message-queue,C++,Boost,Message Queue,是否有人有一些示例代码显示序列化std::字符串的管道,通过boost::interprocess::message_队列将其发送并再次将其取出?您需要序列化数据,因为boost::interprocess::message_队列在字节数组上运行。如果所有消息都是字符串,只需执行以下操作: size_t const max_msg_size = 0x100; boost::interprocess::message_queue q(..., max_msg_size); // sending
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);
解决方案可以是编写一个函数,该函数的输入是要发送的对象(例如,可变大小的字符串),输出是固定大小的对象容器 比如:
int Tokenize(std::vector<MessageToken>&, const Message&) const;
int Merge(Message&, const std::vector<MessageToken>&) const;
int标记化(std::vector&,const Message&)const;
int Merge(Message&,const std::vector&)const;
然后,这些固定大小的对象可以在消息队列中发送/提取
与Maxim的解决方案相比,它的优点是不需要指定max\u msg\u size
参数