Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass complex objects ( std::string ) through boost::interprocess::message queue

Does anybody have some example code showing the pipeline of serializing a std::string sending it through a boost::interprocess::message_queue and getting it back out again?

like image 649
bradgonesurfing Avatar asked Oct 21 '25 13:10

bradgonesurfing


1 Answers

You need to serialize your data because boost::interprocess::message_queue operates on byte arrays. If all your messages are strings, just do:

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);
like image 74
Maxim Egorushkin Avatar answered Oct 23 '25 04:10

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!