boost.asio的异步回调函数是否在同一线程中执行

boost.asio中有async_read_some,async_accept,async_write_some,async_connect这几个函数,那么他们的回调函数都在同一线程吗?如果io_context在单线程运行,那么是的,测试如下。

class LIBTEST_BOOST_DLL TcpServer
{
public:
	typedef  boost::asio::ip::tcp::socket tcp_socket_t;
	typedef  boost::asio::ip::tcp::acceptor tcp_acceptor_t;
	typedef  boost::asio::ip::tcp::endpoint tcp_endpoint_t;
	typedef  boost::asio::io_context io_context_t;
	TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint);
	void Accept();
	void ReadHandler(tcp_socket_t *sock);
private:
	tcp_acceptor_t acceptor_;
	io_context_t *io_;
	std::vector<char> buf_;
	uint64_t cnt_ = 0;
};
class LIBTEST_BOOST_DLL TcpClient
{
public:
	typedef  boost::asio::ip::tcp::socket tcp_socket_t;
	typedef  boost::asio::ip::tcp::endpoint tcp_endpoint_t;
	typedef  boost::asio::io_context io_context_t;
	TcpClient(io_context_t &io);
	void Connect(const tcp_endpoint_t& endpoint);
	void Write();
private:
	tcp_socket_t sock_;
	io_context_t  *io_;
	uint64_t cnt_ = 0;
};

TcpServer::TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint)
	:acceptor_(io,endpoint),io_(&io)
{

}
void TcpServer::ReadHandler(tcp_socket_t *sock)
{
	char *buf = new char[1024];
	sock->async_read_some(boost::asio::buffer(buf,1024), [sock, this,buf](const std::error_code &ec, std::size_t sz) {
		delete buf;
		if (ec) {
			delete sock;
			cout << "value=" << ec.value() << ",msg=" << ec.message()<< endl;
			return;
		}
		//cout << "[" << this_thread::get_id() << "] " << &buf_[0];// << endl;
		cnt_++;
		cout << "[threadId=" << this_thread::get_id() << "] " << "async_read_some callback cnt=" << cnt_ << ",size=" << sz << endl;
		ReadHandler(sock);
	});
}
void TcpServer::Accept()
{
	
	//auto sock = make_shared<tcp_socket_t>(*io_);
	auto sock = new tcp_socket_t(*io_);
	auto acceptHandler = [this,sock](const std::error_code &ec) {
		if (ec) {
			cout << "accept error " << endl;
			return;
		}
		cout << "[threadId=" << this_thread::get_id() << "]";
		cout << " async_accept callback client from: ";
		cout << sock->remote_endpoint().address() <<":" << sock->remote_endpoint().port() << endl;
		ReadHandler(sock);
		Accept();
	};
	acceptor_.async_accept(*sock, acceptHandler);

}

TcpClient::TcpClient(io_context_t &io)
	:sock_(io),io_(&io)
{
}
void TcpClient::Write()
{
	auto buf = new char[1024];
	memset(buf,'q',1024);
	sock_.async_write_some(boost::asio::buffer(buf,1024), [this,buf](const std::error_code &ec, std::size_t sz) {
		delete buf;
		if (ec) {
			cout << "[threadId=" << this_thread::get_id() << "] " << "write error" << endl;
			return;
		}
		cnt_++;
		cout << "[threadId=" << this_thread::get_id() << "] " << "async_write_some callback cnt=" << cnt_ << ",size=" << sz<< endl;
	});
	
}
void TcpClient::Connect(const tcp_endpoint_t& endpoint)
{
	auto connHandler = [this,endpoint](const std::error_code &ec) {
		if (ec) {
			cout << "connect error " << "[threadId=" <<  this_thread::get_id() << "]"<<endl;
			Connect(endpoint);
			return;
		}
		cout << "[threadId=" << this_thread::get_id() << "] ";
		cout << "async_connect callback to ";
		cout << this->sock_.remote_endpoint().address();
		cout << " success" << endl;;
		std::thread t([this] {
			for(int i=0;i<1;i++)
			Write();
		});
		t.detach();
	};
	sock_.async_connect(endpoint, connHandler);
}
int main(int argc, char *argv[])
{

	boost::asio::io_context io;
	
	boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 1234);
	TcpServer tcp(io,endpoint);
	TcpClient client(io);
	tcp.Accept();
	client.Connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),1234));
	boost::asio::io_context::work worker(io);
	io.run();

	
#ifdef WIN32
	system("pause");
#endif
	return 0;
}

回调函数的threadId均相同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值