asio中的定时器steady_timer和deadline_timer

  1. steady_timer的expires_from_now函数参数必须使用std::chrono
  2. deadline_timer的expires_from_now函数参数必须使用boost::posix_time
  3. deadline_timer使用的计量时间是系统时间,它是存在 trap 的, 如果 deadline_timer 正在做 time wait, 在系统时间被外部修改的时候, 是会导致deadline_timer 产生不可预期的影响的, 因为deadline_timer 是受系统时间影响的. 如果需要使用一个稳定不受系统时间影响的定时器建议使用 steady_timer.

同步定时器

cpp 复制代码
/**
 * @brief 测试定时器同步调用
 */
#include <iostream>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>

using namespace boost;

int main()
{  
    asio::io_service io;
    asio::steady_timer timer(io, std::chrono::seconds(3));
    timer.wait();
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

异步定时器

cpp 复制代码
/**
 * @brief 测试定时器异步调用
 */
#include <iostream>
#include <chrono>
#include <thread>

#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace boost;

asio::io_service *g_io = nullptr;

void Print(const system::error_code &ec, asio::steady_timer *timer, int *count)
{
	if (ec)
		return;
	
	if (*count < 3)
	{
		std::cout << "Count: " << *count << std::endl;
		++(*count);
		timer->expires_from_now(std::chrono::milliseconds(1000));
		// timer->expires_at(timer->expires_at() + std::chrono::seconds(1)));
		timer->aysnc_wait(boost::bind(&Print, boost::asio::placeholders::error, timer, count));
	}
	else
	{
		g_io->stop();
	}
}

int DoProcess(asio::steady_timer *timer, int* count)
{
	sleep(1);
	timer->aysnc_wait(boost::bind(&Print, boost::asio::placeholders::error, timer, count));
	sleep(8);
	return 0;
}

int main(int argc, char * argv[])
{  
    int count = 0;
    asio::io_service io;
	asio::io_service::work work(io);
    asio::steady_timer timer(io, std::chrono::seconds(3));
	g_io = &io;
	
	// 这里使用指针传递 可以使用std::ref(count)传递引用
	std::thread t(DoProcess, &timer, &count);
	std::cout << "run before..." << std::endl;
    io.run();
	std::cout << "run after..." << std::endl;
    if (t.joinable())
		t.join();
	
    return 0;
}

work和io_service关系

由于io_service并不会主动创建调度线程,需要我们手动分配,常见的方式是给其分配一个线程,然后执行run函数。但run函数在io事件完成后会退出,线程会终止,后续基于该对象的异步io任务无法得到调度。

解决这个问题的方法是通过一个boost::asio::io_service::work对象来守护io_service。这样,即使所有io任务都执行完成,也不会退出,继续等待新的io任务。

cpp 复制代码
boost::asio::io_service io;
boost::asio::io_service::work work(io);
io.run();

deadline_timer的用法参考:

https://blog.csdn.net/Erice_s/article/details/129965005

相关推荐
Dream it possible!2 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴2 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程2 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空4 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7744 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白15 小时前
C++——list模拟实现
开发语言·c++
程序员老舅5 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1115 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s7 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭7 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode