简体   繁体   中英

A recursive function or race condition inside a Class?

In the following code, the private function handle_read_content uses asio::async_read which depends on the handle_read_content . Could this invoke recursive behaviour or create a race condition?

class client
{
public:
    //constrcutor, io_service,   server name?  path?

    client(asio::io_service& io_service,
           const std::string& server, const std::string& path)
    : resolver_(io_service),
    socket_(io_service)
    {
        //form request, connection close header server close the socket
        //std:ostream reuest_stream


        // Form the request. We specify the "Connection: close" header so that the
        // server will close the socket after transmitting the response. This will
        // allow us to treat all data up until the EOF as the content.
        std::ostream request_stream(&request_);
        request_stream << "GET " << path << " HTTP/1.0\r\n";
        request_stream << "Host: " << server << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";

        // Start an asynchronous resolve to translate the server and service names
        // into a list of endpoints.
        //tcp:resolver:query

        tcp::resolver::query query(server, "http");

        resolver_.async_resolve(query,
                                boost::bind(&client::handle_resolve, this,
                                            asio::placeholders::error,
                                            asio::placeholders::iterator));
    }

private:


    void handle_read_content(const asio::error_code& err)
    {
        if (!err)
        {
            // Write all of the data that has been read so far.
            std::cout << &response_;

            // Continue reading remaining data until EOF.
            asio::async_read(socket_, response_,
                             asio::transfer_at_least(1),
                             boost::bind(&client::handle_read_content, this,
                                         asio::placeholders::error));
        }
        else if (err != asio::error::eof)
        {
            std::cout << "Error: " << err << "\n";
        }
    }

    tcp::resolver resolver_;
    tcp::socket socket_;
    asio::streambuf request_;
    asio::streambuf response_;
};

This is not recursive, the handle_read_content is a callback function that will be invoked when the async_read operation completes.

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/async_read/overload1.html

This is the way async_read is supposed to work, here is what it is doing:

  1. read some data
  2. boost calls call handle_read_content when the read operation is complete
  3. Which invokes a new async_read... and the process starts again... until an error occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM