Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:56:39+00:00 2026-06-08T00:56:39+00:00

I am learning to use Boost ASIO. Here is some code copied from the

  • 0

I am learning to use Boost ASIO. Here is some code copied from the the chat example given along with Boost ASIO documentation,

typedef std::deque<chat_message> chat_message_queue;

class chat_client
{
    public:
        chat_client(boost::asio::io_service& io_service,
                tcp::resolver::iterator endpoint_iterator)
            : io_service_(io_service),
            socket_(io_service)
    {
        boost::asio::async_connect(socket_, endpoint_iterator,
                boost::bind(&chat_client::handle_connect, this,
                    boost::asio::placeholders::error));
    }

        void write(const chat_message& msg)
        {
            io_service_.post(boost::bind(&chat_client::do_write, this, msg));
        }

        void close()
        {
            io_service_.post(boost::bind(&chat_client::do_close, this));
        }

    private:

        void handle_connect(const boost::system::error_code& error)
        {
            //Implementation
        }

        void handle_read_header(const boost::system::error_code& error)
        {
            //Implementation
        }

        void handle_read_body(const boost::system::error_code& error)
        {
            //Implementation
        }

        void do_write(chat_message msg)
        {
            bool write_in_progress = !write_msgs_.empty();
            write_msgs_.push_back(msg);
            if (!write_in_progress)
            {
                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));
            }
        }



        void handle_write(const boost::system::error_code& error)
        {
            //Implementation
        }

        void do_close()
        {
            socket_.close();
        }

    private:
        boost::asio::io_service& io_service_;
        tcp::socket socket_;
        chat_message read_msg_;
        chat_message_queue write_msgs_;
};
  1. The writes are asynchronous and there is no use of locks around the member variables write_msgs_ and read_msgs_ . Shouldn’t there be an issue with concurrency here?

  2. Is it safe to make calls to post from the thread that runs io_service::run? What about dispatch? What about making the same calls from threads that are not running io_service::run?

  3. In doSend(), why are they pushing the message into write_msgs_ instead of directly sending it? Also in the same function why are they checking if the write_msgs_ was empty and only if it was not, proceeding to send? Does write_msgs_.empty() = false mean a write is going on? How?

  4. If do_write() gets invoked only in one thread then why do I need a queue to maintain a sequence of send? Wouldn’t the io_service finish the tasks at hand and then do the asynchronous operation called by do_write? Will using a dispatch instead of post make a difference in the example mentioned above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:56:41+00:00Added an answer on June 8, 2026 at 12:56 am
    1. Although the writes are asynchronous, there’s no multithreading here: do_write() gets invoked in one thread. Of course, the buffer being sent must be alive and unchanged until the completion handler is invoked.

    2. It’s safe to call post() and dispatch() from any thread. Read “thread safety” section of io_service documentation.

    3. If async_write is in progress, and you call async_write on the same socket again, the order in which the data would be sent is undefined. In other words, the data will be messed up. The most simple way to work-around this is to make a queue of messages: every time async_write gets completed, issue another async_write. (By the way, the same is applicable to async_read.)
      Why does write_msgs_.empty() = false mean a write is going on? Because as long as write_msgs_ is non-empty, handle_write (completion handler of the previous async_write) issues another async_write. This loop breaks when write_msgs_ is empty.

    4. Please read what the documentation says about async_write:

      This operation is implemented in terms of zero or more calls to the
      stream’s async_write_some function, and is known as a composed
      operation
      . The program must ensure that the stream performs no other
      write operations (such as async_write, the stream’s async_write_some
      function, or any other composed operations that perform writes) until
      this operation completes.

    As for dispatch vs post – as far as I see, in the above example they are interchangeable. Using post is essential if we don’t want the functor being posted to be invoked synchronously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning how to use Boost:asio library with Serial Port. I wrote some code
I am learning to use xmlhttprequest/AJAX. In this sample code from w3schools, I do
I am learning how to use the boost asio libraries and I am using
Learning boost, and compiled their daytime server client example . Since I cant use
Start with the code: #include <iostream> #include <string> #include <map> #include <boost/asio.hpp> typedef std::map<boost::asio::ip::address,
I'm learning the use of boost smart pointers but I'm a bit confused about
I am learning to use DelgateCommand from Prism.... In my UI, I have my
I am learning to use json_decode . I want to try out some php
Learning to use Ruby Threads for transportability of code between different OS platforms. The
I'm working on learning to use boost threads. I'm trying to make a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.