Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to call boost io_service.post

I have the following functions:

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);
}

So I call sendMessagein one thread and onSendMessage will be invoked in main thread.

The question is will be message string copied by boost in this case or not. If no - how can I pass string into onSendMessage function and be sure that there will no memory leak and message string is valid, not deleted object?

like image 294
Artem Avatar asked Jan 18 '26 12:01

Artem


1 Answers

onSendMessage will be invoked in one of the threads that execute m_xIOService::run - not in the main thread.

All bind arguments are copied, so message will be copied as well. Whenever you want to pass bind parameters by reference, use boost::ref wrapper.

like image 130
Igor R. Avatar answered Jan 21 '26 03:01

Igor R.



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!