I'm trying to understand the boost::asio but have some problems with it. In this example, please correct me if I'm wrong.
The message is given by reference to the write() method and by value to do_write(). So I think this is ok, even though the do_write is done by io_service.post, the boost::bind is binding the message by value.
But why is there no mutex for the write_msgs_ queue, since std::deque might move around or copy its elements, if needed and io_service::run has it's own thread, it is not ensured the data is consistent.
Is it not better to do it by pointer. If the messages are to long they have always to be copied by value. But with new and delete they will be created before and deleted after sending. Then I would do the send like so
code
boost::asio::async_write(socket_,
boost::asio::buffer(*write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&chat_client::handle_write, this,
boost::asio::placeholders::error));