boost asio 疑问

wisefree · 2024-2-15 17:40:24 · 60 次点击
最近在看 asio 的帮助文档,看到了官方的 demo ,请问大家 conn 的引用计数增加为什么是 1 ,2 ,3 ,4 呢?

``` c++
std::cout << "3: " << new_connection.use_count() << std::endl;
```
bind 可以增加引用计数的话,这个位置 conn 的引用计数应该是 2 吧,因为 start_accept 运行完毕后,引用计数要减 1


``` c++

//
// serfer.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_serfer
{
public:
  tcp_serfer(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_serfer::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_serfer serfer(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`

输出为:
``` html
1: 1
2: 2
3: 3
1: 1
2: 2
4: 4
conn descontruct
```
举报· 60 次点击
登录 注册 站外分享
2 条回复  
byaiu 小成 2024-2-15 21:00:19
tcp_connection::start 里的 shared_from_this 给 use_count 加了 1.
同时,start_acept 里是另一个新的 tcp_connection ,和 tcp_serfer::handle_accept 里的 new_connection 不是一个。
cnbatch 初学 2024-2-16 15:55:50
这是 boost 的“锅”/ bug 。

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

1: 1
2: 2
3: 2
1: 1
2: 2
4: 3
conn descontruct
返回顶部