Boost库为C++提供了强大的支持,尤其在多线程和网络编程方面。其中,Boost.Asio库是一个基于前摄器设计模式的库,用于实现高并发和网络相关的开发。Boost.Asio核心类是io_service,它相当于前摄模式下的Proactor角色。所有的IO操作都需要通过io_service来实现。

在异步模式下,程序除了发起IO操作外,还需要定义一个用于回调的完成处理函数。io_service将IO操作交给操作系统执行,但它不同步等待,而是立即返回。调用io_servicerun成员函数可以等待异步操作完成。当异步操作完成时,io_service会从操作系统获取结果,再调用相应的处理函数(handler)来处理后续逻辑。

这种异步模型的优势在于它能够更有效地利用系统资源,避免线程阻塞,提高程序的并发性能。Boost.Asio的设计让开发者能够以高效的方式开发跨平台的并发网络应用,使C++在这方面能够与类似Java等语言相媲美。

ASIO异步定时器

boost::asio::deadline_timer 是 Boost.Asio 库中用于处理定时器的类。它允许你在一段时间后或在指定的时间点触发回调函数。deadline_timer 通常与 io_service 配合使用,以实现异步定时器功能。

以下是 boost::asio::deadline_timer 的一些重要概念和方法:

构造函数: deadline_timer 的构造函数通常需要一个 io_service 对象和一个时间参数。时间参数可以是相对时间(相对于当前时间的一段时间间隔)或绝对时间(具体的时刻)。

cppCopy codeboost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));

expires_from_now 方法: 通过调用 expires_from_now 方法,可以设置相对于当前时间的时间间隔,来定义定时器的到期时间。

cppCopy code
timer.expires_from_now(boost::posix_time::seconds(10));

expires_at 方法: 通过调用 expires_at 方法,可以设置定时器的到期时间为一个具体的时刻。

cppCopy codeboost::posix_time::ptime expiryTime = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(10);
timer.expires_at(expiryTime);

async_wait 方法: async_wait 方法用于启动异步等待定时器的到期。它接受一个回调函数作为参数,该回调函数将在定时器到期时被调用。

cppCopy codevoid timerCallback(const boost::system::error_code& /*e*/)
{
std::cout << "Timer expired!" << std::endl;
} timer.async_wait(boost::bind(timerCallback, boost::asio::placeholders::error));

取消定时器: 你可以通过调用 cancel 方法来取消定时器,以停止它在到期时触发回调函数。

cppCopy code
timer.cancel();

boost::asio::deadline_timer 提供了一种灵活和强大的方式来处理异步定时器操作,使得你可以方便地执行定时任务、调度操作或执行周期性的工作。

#include <iostream>
#include <boost/asio.hpp> using namespace std;
using namespace boost::asio; void handler(const boost::system::error_code &ec)
{
cout << "hello lyshark A" << endl;
} void handler2(const boost::system::error_code &ec)
{
cout << "hello lyshark B" << endl;
} int main(int argc,char *argv)
{
boost::asio::io_service service; boost::asio::deadline_timer timer(service, boost::posix_time::seconds(5));
timer.async_wait(handler); boost::asio::deadline_timer timer2(service, boost::posix_time::seconds(10));
timer2.async_wait(handler2); service.run(); std::system("pause");
return 0;
}

上述代码运行后,会分别间隔5秒及10秒,用来触发特定的handler函数,效果如下图所示;

在 Boost.Asio 中,io_service::run() 是一个关键的方法,它用于运行 I/O 服务的事件循环。通常,run() 方法会一直运行,直到没有更多的工作需要完成,即直到没有未完成的异步操作。

如果多个异步函数同时调用同一个 io_servicerun() 方法,可以考虑将 run() 方法单独摘出来,以便在线程函数中多次调用。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp> using namespace std;
using namespace boost::asio; void handler(const boost::system::error_code &ec)
{
cout << "hello lyshark A" << endl;
} void handler2(const boost::system::error_code &ec)
{
cout << "hello lyshark B" << endl;
} boost::asio::io_service service; void run()
{
service.run();
} int main(int argc,char *argv)
{
boost::asio::deadline_timer timer(service, boost::posix_time::seconds(5));
timer.async_wait(handler); boost::asio::deadline_timer timer2(service, boost::posix_time::seconds(10));
timer2.async_wait(handler2); boost::thread thread1(run);
boost::thread thread2(run); thread1.join();
thread2.join(); std::system("pause");
return 0;
}

上述代码的运行效果与第一个案例一致,唯一的不同在于,该案例中我们通过boost::thread分别启动了两个线程,并通过join()分别等待这两个线程的执行结束,让异步与线程分离。

通过多次触发计时器,实现重复计时器功能,如下代码使用 Boost.Asio 实现了一个异步定时器的例子。

该程序定义了一个计数器 count,并创建了一个 steady_timer 对象 io_timer,设置其到期时间为 1 秒。然后,通过 io_timer.async_wait 启动了一个异步等待操作,该操作在计时器到期时调用 print 函数。

print 函数中,首先判断计数器是否小于 5,如果是,则输出计数器的值,并将计时器的到期时间延迟 1 秒。然后,再次启动新的异步等待操作,递归调用 print 函数。当计数器达到 5 时,停止了 io 对象,这会导致 io.run() 返回,程序退出。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp> // 定义输出函数
void print(const boost::system::error_code &,boost::asio::steady_timer * io_timer, int * count)
{
// 如果计时器等于4也就是循环5此后自动停止
if (*count < 5)
{
std::cout << "Print函数计数器: " << *count << std::endl;
++(*count); // 将计时器到期时间向后延时1秒
io_timer->expires_at(io_timer->expiry() + boost::asio::chrono::seconds(1)); // 启动一个新的异步等待
io_timer->async_wait(boost::bind(print,boost::asio::placeholders::error, io_timer, count));
}
} int main(int argc, char *argv)
{
boost::asio::io_context io;
int count = 0; // 定义IO时间为1秒
boost::asio::steady_timer io_timer(io, boost::asio::chrono::seconds(1)); // 绑定并调用print函数
io_timer.async_wait(boost::bind(print, boost::asio::placeholders::error, &io_timer, &count)); io.run();
std::cout << "循环已跳出,总循环次数: " << count << std::endl; std::system("pause");
return 0;
}

运行上述代码,输出效果如下图所示,通过计数器循环执行特定次数并输出,每次间隔为1秒。

与之前的代码相比,如下所示的版本使用了一个类 print 来封装定时器操作。

与之前版本相比的主要不同点:

  1. 类的引入: 引入了 print 类,将定时器和计数器等相关的操作封装到了一个类中,提高了代码的封装性和可读性。
  2. 构造函数和析构函数:print 类中使用构造函数初始化 timer_ 定时器,而在析构函数中打印最终循环次数。这样的设计使得对象的创建和销毁分别与初始化和清理相关的操作关联起来。
  3. 成员函数 run_print 使用了成员函数 run_print 作为定时器回调函数,无需再使用 boost::bind 绑定 this 指针,直接使用类的成员变量,提高了代码的简洁性。
  4. 对象的创建和运行:main 函数中,直接创建了 print 对象 ptr,并通过 io.run() 来运行异步操作,无需手动调用 async_wait。这种方式更加面向对象,将异步操作和对象的生命周期绑定在一起。
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp> class print
{
private:
boost::asio::steady_timer timer_;
int count_; public:
// 构造时引用io_context对象,使用它初始化timer
print(boost::asio::io_context& io) : timer_(io, boost::asio::chrono::seconds(1)), count_(0)
{
// 使用 bind 绑定当前对象的 this 指针,利用成员 count 控制计时器的执行
timer_.async_wait(boost::bind(&print::run_print, this));
} // 在析构中打印结果
~print()
{
std::cout << "循环已跳出,总循环次数: " << count_ << std::endl;
} // 作为类的成员函数,无需再传入参数,直接使用当前对象的成员变量
void run_print()
{
if (count_ < 5)
{
std::cout << "Print函数计时器: " << count_ << std::endl;
++count_; timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1));
timer_.async_wait(boost::bind(&print::run_print, this));
}
}
}; int main(int argc, char *argv)
{
boost::asio::io_context io;
print ptr(io);
io.run(); std::system("pause");
return 0;
}

这个输出效果与之前基于过程的保持一致,其他的并无差异;

如下版本的代码相对于之前的版本引入了 io_context::strand 来保证定时器回调函数的串行执行,避免了多个线程同时执行 print1print2 导致的竞态条件。

与之前版本相比的主要不同点:

  1. io_context::strand 的引入: 引入了 io_context::strand 对象 strand_,用于确保 print1print2 的回调函数在同一线程内按序执行。io_context::strand 在多线程环境中提供了同步操作,确保绑定到 strand_ 上的操作不会同时执行。
  2. bind_executor 的使用:async_wait 中使用了 boost::asio::bind_executor 函数,将定时器的回调函数与 strand_ 绑定,保证了异步操作的执行在 strand_ 内。这样可以确保 print1print2 不会在不同线程中同时执行。
  3. 多线程运行 io_context 引入了两个子线程 tt1,分别调用 io_context::run 来运行 io_context。这样可以使 io_context 在两个独立的线程中运行,增加了并发性。
  4. 线程的 Join:main 函数中,通过 t.join()t1.join() 等待两个子线程执行完成后再退出程序。这样确保了 main 函数在所有线程都完成后再结束。

总体而言,这个版本通过引入 io_context::strand 以及多线程运行 io_context,解决了异步操作可能导致的竞态条件,增强了程序的并发性。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp> class print
{
public:
print(boost::asio::io_context& io) : strand_(io), // strand用于控制handler的执行
timer1_(io, boost::asio::chrono::seconds(1)), // 运行两个计时器
timer2_(io, boost::asio::chrono::seconds(1)),
count_(0)
{
// 启动异步操作时,每个 handler 都绑定到 strand 对象
// bind_executor() 返回一个新的 handler,它将自动调度其包含的 print::print1
// 通过将 handler 绑定到同一个 strand,保证两个print不会同时执行
timer1_.async_wait(boost::asio::bind_executor(strand_,boost::bind(&print::print1, this)));
timer2_.async_wait(boost::asio::bind_executor(strand_,boost::bind(&print::print2, this)));
} void print1()
{
if (count_ < 10)
{
std::cout << "Print 1: " << count_ << std::endl;
++count_; timer1_.expires_at(timer1_.expiry() + boost::asio::chrono::seconds(1));
timer1_.async_wait(boost::asio::bind_executor(strand_,boost::bind(&print::print1, this)));
}
} void print2()
{
if (count_ < 10)
{
std::cout << "Print 2: " << count_ << std::endl;
++count_; timer2_.expires_at(timer2_.expiry() + boost::asio::chrono::seconds(1));
timer2_.async_wait(boost::asio::bind_executor(strand_,boost::bind(&print::print2, this)));
}
} private:
boost::asio::io_context::strand strand_;
boost::asio::steady_timer timer1_;
boost::asio::steady_timer timer2_;
int count_;
}; int main(int argc,char *argv[])
{
// 第一个线程
boost::asio::io_context io;
print ptr(io); // 定义两个子线程
boost::thread t(boost::bind(&boost::asio::io_context::run, &io));
boost::thread t1(boost::bind(&boost::asio::io_context::run, &io)); io.run();
t.join();
t1.join(); std::system("pause");
return 0;
}

输出效果如下图所示;

ASIO异步网络通信

异步通信的原理与同步通信不同,主要体现在程序对IO请求的处理上。在异步状态下,程序发起IO请求后会立即返回,无需等待IO操作完成。无论IO操作成功还是失败,程序都可以继续执行其他任务,不会被阻塞。当IO请求被执行完成后,系统会通过回调函数的方式通知调用者,使其能够获取操作的状态或结果。

这种异步通信的机制带来了一些优势:

  1. 提高并发性: 在异步模式下,程序在等待IO操作完成的过程中不会阻塞,可以继续执行其他任务,充分利用了宝贵的CPU时间。这使得程序更容易实现高并发,同时处理多个IO操作。
  2. 节省时间: 由于程序不需要等待IO操作完成,可以更加高效地利用时间。在同步模式下,程序必须等待每个IO操作的完成,而在异步模式下,可以在等待的时间内执行其他任务,提高了整体效率。
  3. 提高系统响应性: 异步通信使得程序能够更灵活地响应IO事件,及时处理完成的IO操作。这对于需要快速响应用户请求的系统非常重要,如网络通信、图形用户界面等。
  4. 减少资源浪费: 在异步模式下,程序可以通过回调函数获取IO操作的结果,而无需通过轮询或其他方式一直等待。这减少了对系统资源的浪费,提高了系统的效率。

异步通信的原理在于通过非阻塞的方式发起IO请求,充分利用等待IO完成的时间,通过回调函数的方式获取IO操作的结果,以提高程序的并发性、响应性和效率。

使用Boost.Asio库实现简单的异步TCP服务器。

对代码的主要分析:

  1. IOService 结构体:

    • 该结构体负责管理 io_serviceacceptor
    • 构造函数初始化 io_serviceacceptor 对象。acceptor 用于监听连接请求。
    • start() 函数启动异步等待连接操作,当有客户端连接请求时,触发 accept_handler
  2. start() 函数:
    • start() 函数中,通过 async_accept 异步等待连接请求,当有客户端连接请求时,会触发 accept_handler 函数。
    • 创建了一个新的 tcp::socket 对象,并使用 async_accept 异步等待连接请求。
    • accept_handler 函数被绑定,负责处理连接成功后的操作。
  3. accept_handler 函数:
    • 当有客户端连接成功时,该函数会被调用。
    • 递归调用 start(),以便继续等待新的连接请求。
    • 输出远程客户端的IP地址。
    • 创建一个字符串指针 pstr,并发送 "hello lyshark" 给客户端。
  4. write_handler 函数:
    • 当异步写操作完成时,该函数被调用。
    • 输出已发送的信息。
  5. main 函数:
    • 创建了一个 io_service 对象和 IOService 对象 server
    • 调用 server.start() 启动服务器。
    • 调用 io.run() 启动 IO 服务,使其保持运行状态,直到所有异步操作完成。

整体而言,这个程序通过异步的方式接受客户端连接,并在连接建立后异步发送消息给客户端。使用 Boost.Asio 提供的异步操作可以实现高效的并发网络编程。

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp> using namespace boost::asio;
using boost::system::error_code;
using ip::tcp; struct IOService
{
IOService(io_service &io) :m_iosev(io), m_acceptor(io, tcp::endpoint(tcp::v4(), 80))
{
std::cout << "执行构造函数" << std::endl;
}
void start()
{
// 非阻塞等待连接
boost::shared_ptr<tcp::socket> psocket(new tcp::socket(m_iosev));
// 绑定 IOService::accept_handler 当有请求进来时,自动回调到绑定accept_handler函数上
m_acceptor.async_accept(*psocket,boost::bind(&IOService::accept_handler, this, psocket, _1));
} // 有客户端连接时accept_handler触发
void accept_handler(boost::shared_ptr<tcp::socket> psocket, error_code ec)
{
if (ec) return; // 再次递归调用start()函数继续等待新连接进入
start(); // 显示远程IP
std::cout << "远端IP: " << psocket->remote_endpoint().address() << std::endl; // 发送信息(非阻塞)
boost::shared_ptr<std::string> pstr(new std::string("hello lyshark")); // 绑定 IOService::write_handler 回调函数,当发送完成后,自动触发 write_handler
psocket->async_write_some(buffer(*pstr),boost::bind(&IOService::write_handler, this, pstr, _1, _2));
} // 异步写操作完成后write_handler触发
void write_handler(boost::shared_ptr<std::string> pstr,error_code ec, size_t bytes_transferred)
{
if (!ec)
std::cout << *pstr << " 已发送" << std::endl;
} private:
io_service &m_iosev;
ip::tcp::acceptor m_acceptor;
}; int main(int argc, char* argv[])
{
io_service io;
IOService server(io);
server.start();
io.run();
return 0;
}

客户端代码

#include <iostream>
#include <string>
#include <boost/asio.hpp> using namespace boost::asio; int main(int argc, char *argv[])
{
io_service io_service;
ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 1000);
ip::tcp::socket socket(io_service);
socket.connect(ep); char buffer[1024] = { 0 };
socket.read_some(boost::asio::buffer(buffer));
std::cout << buffer << std::endl; std::system("pause");
return 0;
}

C++ Boost 异步网络编程基础的更多相关文章

  1. python网络编程基础(线程与进程、并行与并发、同步与异步、阻塞与非阻塞、CPU密集型与IO密集型)

    python网络编程基础(线程与进程.并行与并发.同步与异步.阻塞与非阻塞.CPU密集型与IO密集型) 目录 线程与进程 并行与并发 同步与异步 阻塞与非阻塞 CPU密集型与IO密集型 线程与进程 进 ...

  2. Java网络编程基础(Netty预备知识)

    今天在家休息,闲来无事,写篇博客,陶冶下情操~~~ =================我是分割线================ 最近在重新学习Java网络编程基础,以便后续进行Netty的学习. 整 ...

  3. 用Netty开发中间件:网络编程基础

    用Netty开发中间件:网络编程基础 <Netty权威指南>在网上的评价不是很高,尤其是第一版,第二版能稍好些?入手后快速翻看了大半本,不免还是想对<Netty权威指南(第二版)&g ...

  4. C#网络编程基础知识

    C#网络编程基础知识一 1.IPAddress类 用于表示一个IP地址.IPAddress默认构造函数 public IPAddress(long address);一般不用 其中Parse()方法最 ...

  5. iOS开发网络篇—网络编程基础

    iOS开发网络篇—网络编程基础 一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过 ...

  6. Android 网络编程基础之简单聊天程序

    前一篇讲了Android的网络编程基础,今天写了一个简单的聊天程序分享一下 首先是服务端代码: package com.jiao.socketdemo; import java.io.Buffered ...

  7. 服务器编程入门(4)Linux网络编程基础API

      问题聚焦:     这节介绍的不仅是网络编程的几个API     更重要的是,探讨了Linux网络编程基础API与内核中TCP/IP协议族之间的关系.     这节主要介绍三个方面的内容:套接字( ...

  8. Linux 高性能服务器编程——Linux网络编程基础API

    问题聚焦:     这节介绍的不仅是网络编程的几个API     更重要的是,探讨了Linux网络编程基础API与内核中TCP/IP协议族之间的关系.     这节主要介绍三个方面的内容:套接字(so ...

  9. Python网络编程基础pdf

    Python网络编程基础(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1VGwGtMSZbE0bSZe-MBl6qA 提取码:mert 复制这段内容后打开百度网盘手 ...

  10. 【网络编程1】网络编程基础-TCP、UDP编程

    网络基础知识 网络模型知识 OSI七层模型:(Open Systems Interconnection Reference Model)开放式通信系统互联参考模型,是国际标准化组织(ISO)提出的一个 ...

随机推荐

  1. 浅谈locust 性能压测使用

    1. 基本介绍 Locust是一个开源的负载测试工具,用于模拟大量用户并发访问一个系统或服务,以评估其性能和稳定性.编写语言为Python,可通过Python来自定义构建性能压测场景脚本.Locust ...

  2. GPT-4测评,大家先别急,图片输入还没来

    昨天GPT-4朋友圈刷屏,我更新了一篇小文章,极简罗列GPT-4的一些情报: 1 ChatGPT Plus用户才可试用GPT-4 2 试用阶段每四小时最多100条信息 3 知识库还是2021年 4 上 ...

  3. C# golang 开10000个无限循环的性能

    知乎上有人提了个问题,可惜作者已把账号注销了. 复制一下他的问题,仅讨论技术用,侵删. 问题 作者:知乎用户fLP2gX 链接:https://www.zhihu.com/question/63484 ...

  4. 创新推出 | Serverless 调试大杀器:端云联调

    背景 说起当前最火一个技术, 不可避免地讨论到一个概念: Serverless.作为一种新型的应用架构,Serverless 让我们摆脱了维护基础设施的繁琐,只需要上传代码包或者镜像, 即可得到一个弹 ...

  5. SpringCloud学习 系列六、服务平滑离线

    系列导航 SpringCloud学习 系列一. 前言-为什么要学习微服务 SpringCloud学习 系列二. 简介 SpringCloud学习 系列三. 创建一个没有使用springCloud的服务 ...

  6. 10、SpringBoot-mybatis-plus-druid多源数据

    系列导航 springBoot项目打jar包 1.springboot工程新建(单模块) 2.springboot创建多模块工程 3.springboot连接数据库 4.SpringBoot连接数据库 ...

  7. php开发中常见的漏洞点(一) 基础sql注入

    前言 本系列为小迪2022的学习笔记,仅用于自我记录. 正文 在一般情况下,一个网站的首页大致如下 在上方存在着各种各样的导航标签.链接.而一般情况下网站的导航会用参数进行索引的编写,比如id.pag ...

  8. macOX常用快捷键(结尾有彩蛋)

    macOX的快捷键与windows10有所不同,了解了以后会更加提高我们的工作效率. Mac中主要有四个修饰键,分别是Command,Control,Option和Shift. 一.基本的快捷键: C ...

  9. python之单线程、多线程、多进程

    一.基本概念 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 在当代面向线程设计的计算机结构中,进程是线程的容器.程 ...

  10. cs 保研经验贴 | 英语口试

    很多夏令营都有英语面试环节.但这其实是有迹可循的,多说几遍就熟练了. 无论是笔试面试,还是联系导师 联系 hr,这种自我推销的事情,都会越做越熟练的.希望发表也是如此吧-(来自博零菜鸟的碎碎念-) 目 ...