boost asio 疑问

84 天前
 wisefree

最近在看 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
1473 次点击
所在节点    C++
5 条回复
byaiu
84 天前
tcp_connection::start 里的 shared_from_this 给 use_count 加了 1.
同时,start_acept 里是另一个新的 tcp_connection ,和 tcp_server::handle_accept 里的 new_connection 不是一个。
wisefree
83 天前
@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
83 天前
这是 boost 的“锅”/ bug 。

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

1: 1
2: 2
3: 2
1: 1
2: 2
4: 3
conn descontruct
cnbatch
83 天前
如果 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
83 天前
@cnbatch 太感谢了!昨天运行的时候,真是百思不得其解

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1015746

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX