V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wisefree
V2EX  ›  C++

boost asio 疑问

  •  
  •   wisefree · 72 天前 · 1425 次点击
    这是一个创建于 72 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在看 asio 的帮助文档,看到了官方的 demo ,请问大家 conn 的引用计数增加为什么是 1 ,2 ,3 ,4 呢?

    std::cout << "3: " << new_connection.use_count() << std::endl;
    

    bind 可以增加引用计数的话,这个位置 conn 的引用计数应该是 2 吧,因为 start_accept 运行完毕后,引用计数要减 1

    
    //
    // server.cpp
    // ~~~~~~~~~~
    //
    // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <boost/bind/bind.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/asio.hpp>
    
    using boost::asio::ip::tcp;
    
    std::string make_daytime_string()
    {
      using namespace std; // For time_t, time and ctime;
      time_t now = time(0);
      return ctime(&now);
    }
    
    class tcp_connection
      : public boost::enable_shared_from_this<tcp_connection>
    {
    public:
      typedef boost::shared_ptr<tcp_connection> pointer;
    
      static pointer create(boost::asio::io_context& io_context)
      {
        return pointer(new tcp_connection(io_context));
      }
    
      tcp::socket& socket()
      {
        return socket_;
      }
    
      void start()
      {
        message_ = make_daytime_string();
        
        boost::asio::async_write(socket_, boost::asio::buffer(message_),
            boost::bind(&tcp_connection::handle_write, shared_from_this(),
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
      
      ~tcp_connection() { std::cout << "conn descontruct" << std::endl;}
    
    private:
      tcp_connection(boost::asio::io_context& io_context)
        : socket_(io_context)
      {
      }
    
      void handle_write(const boost::system::error_code& /*error*/,
          size_t /*bytes_transferred*/)
      {
      }
    
      tcp::socket socket_;
      std::string message_;
    };
    
    class tcp_server
    {
    public:
      tcp_server(boost::asio::io_context& io_context)
        : io_context_(io_context),
          acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
      {
        start_accept();
      }
    
    private:
      void start_accept()
      {
        tcp_connection::pointer new_connection =
          tcp_connection::create(io_context_);
        
        std::cout << "1: " << new_connection.use_count() << std::endl;
        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&tcp_server::handle_accept, this, new_connection,
              boost::asio::placeholders::error));
        std::cout << "2: " << new_connection.use_count() << std::endl;
      }
    
      void handle_accept(tcp_connection::pointer new_connection,
          const boost::system::error_code& error)
      {
        std::cout << "3: " << new_connection.use_count() << std::endl;
        if (!error)
        {
          new_connection->start();
        }
    
        start_accept();
        std::cout << "4: " << new_connection.use_count() << std::endl;
      }
    
      boost::asio::io_context& io_context_;
      tcp::acceptor acceptor_;
    };
    
    int main()
    {
      try
      {
        boost::asio::io_context io_context;
        tcp_server server(io_context);
        io_context.run();
      }
      catch (std::exception& e)
      {
        std::cerr << e.what() << std::endl;
      }
    
      return 0;
    }
    
    
    1. 编译完成,运行程序
    2. 新开一个终端,运行 nc 127.0.0.1 13

    输出为:

    1: 1
    2: 2
    3: 3
    1: 1
    2: 2
    4: 4
    conn descontruct
    
    5 条回复    2024-02-16 17:43:07 +08:00
    byaiu
        1
    byaiu  
       72 天前   ❤️ 1
    tcp_connection::start 里的 shared_from_this 给 use_count 加了 1.
    同时,start_acept 里是另一个新的 tcp_connection ,和 tcp_server::handle_accept 里的 new_connection 不是一个。
    wisefree
        2
    wisefree  
    OP
       71 天前
    @byaiu 也解释不了
    ``` c++
    std::cout << "3: " << new_connection.use_count() << std::endl;
    ```

    刚创建 conn ,引用计数是 1

    bind 后,引用计数是 2

    start_accept 运行结束,引用计数减 1 ,那么引用计数是 1

    handle_accept 中,函数参数拷贝,引用计数加 1 ,那么引用计数应该是 2 ,而不是 3
    cnbatch
        3
    cnbatch  
       71 天前   ❤️ 1
    这是 boost 的“锅”/ bug 。

    把 boost 组件换成 C++11 的相同组件和纯 asio ,得到如下结果:

    1: 1
    2: 2
    3: 2
    1: 1
    2: 2
    4: 3
    conn descontruct
    cnbatch
        4
    cnbatch  
       71 天前   ❤️ 1
    如果 OP 想自己做替换,基本上删掉 boost 前缀或者替换成 std 命名空间就行。有几个特殊地方除外:

    boost::asio::placeholders::error
    boost::asio::placeholders::bytes_transferred
    按顺序替换成
    std::placeholders::_1
    std::placeholders::_2

    以及
    boost::system::error_code 替换成 std::error_code
    wisefree
        5
    wisefree  
    OP
       71 天前
    @cnbatch 太感谢了!昨天运行的时候,真是百思不得其解
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1033 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:37 · PVG 02:37 · LAX 11:37 · JFK 14:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.