简体   繁体   English

类中的递归函数或竞争条件?

[英]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 . 在以下代码中,私有函数handle_read_content使用asio::async_read ,它取决于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. 这不是递归的,handle_read_content是一个回调函数,将在async_read操作完成时调用。

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/async_read/overload1.html 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: 这是async_read应该工作的方式,这是它的作用:

  1. read some data 读取一些数据
  2. boost calls call handle_read_content when the read operation is complete 当读取操作完成时,boost调用handle_read_content
  3. Which invokes a new async_read... and the process starts again... until an error occurs. 它将调用新的async_read ...,然后该过程再次开始...,直到发生错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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