boost asio 疑问

查看 42|回复 0
作者:wisefree   
最近在看 asio 的帮助文档,看到了官方的 demo ,请问大家 conn 的引用计数增加为什么是 1 ,2 ,3 ,4 呢?
std::cout
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
#include
#include
#include
#include
#include
#include
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
{
public:
  typedef boost::shared_ptr 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 socket(),
        boost::bind(&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
    std::cout start();
    }
    start_accept();
    std::cout
[ol]
  • 编译完成,运行程序
  • 新开一个终端,运行 nc 127.0.0.1 13
    [/ol]
    输出为:
    1: 1
    2: 2
    3: 3
    1: 1
    2: 2
    4: 4
    conn descontruct
  • 您需要登录后才可以回帖 登录 | 立即注册