Artem
Artem

Reputation: 239

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?

Upvotes: 3

Views: 2914

Answers (1)

Igor R.
Igor R.

Reputation: 15075

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.

Upvotes: 6

Related Questions