简单的几个Boost定时器
boost的asio库里有几个定时的器,老的有 deadline_timer , 还有三个可配合 C++11 的 chrono 使用的 high_resolution_timer 、 steady_timer 和 system_timer 。
老的 deadline_timer 我不太想用了,因为用起来没有后面三个好用。但是后面三个没有 C++ 11 也不好用。
C++ 之父 曾建议 老的程序应该用最新的编译器在C++ 11的标准下重新编译一遍,当然,他还补充说是在成本上允许的情况下,如果你的老程序是长年累月一点点编译 和增长起来的,重新编译可能不现实。 但是新的 C++ 项目,应该优先使用 C++11的标准来实现。
这里仅简单介绍一下 后三个 中的任意一个,因为它们实现相似到很难看出区别。除非在极端的条件下,后三个用哪一个都差不多,如果你不知道用哪个,那就用 steady_timer 吧。
下面看示例:
#include <iostream>
#include <chrono>
#include <thread>
#include <boost/asio.hpp> #include <boost/asio/high_resolution_timer.hpp >
#include <boost/asio/steady_timer.hpp >
#include <boost/asio/system_timer.hpp > class printer {
private:
boost::asio::io_service io_;
boost::asio::steady_timer timer_;
int count_;
void print() {
if (count_ < ) {
std::cout << count_ << "\n";
++count_; timer_.expires_from_now(std::chrono::milliseconds ());
timer_.async_wait(std::bind(&printer::print, this));
}
else
{
std::cout << "Final count is " << count_ << "\n";
delete this;
}
}
void run() {
timer_.expires_from_now(std::chrono::milliseconds ());
timer_.async_wait(std::bind(&printer::print, this));
io_.run();
}
printer()
: timer_(io_),
count_() { }
~printer() { } public: static printer* Create(){
return new printer;
} void start() {
std::thread t;
t = std::thread(std::mem_fn(&printer::run), this);
t.detach();
}
};
void foo()
{
printer *p = printer::Create();
p->start();
}
int main() {
foo();
std::cin.get();
return ;
}
该代码在 windows 下使用 tdm-gcc 4.8.1 配合 boost 1.57.0 编译通过并运行。 链接时需要 wsock32 库 和 boost-system 库 ( -lboost_system -lwsock32)
源代码存为main.cpp,下面是linux下的makefile (centos 7.0 系统自带的 gcc 和 boost 1.5.70
CXX = g++
BIN = timer
OBJ = main.o
LINKOBJ = -pthread -lboost_system
CXXINC =
CXXFLAGS = $(CXXINC) -std=c++ -O3 -Wall
RM = rm -f $(BIN):$(OBJ)
$(CXX) $(CXXFLAGS) $(OBJ) $(LINKOBJ) -o $(BIN) %.o:%.cpp
$(CXX) $(CXXFLAGS) -c $< clean:
$(RM) $(OBJ) $(BIN) rebuild:
make clean
make
定时器的用法也是比较简单的,基本上分三步。创建 io_service , 创建timer 并设置等待时间, 调用wait 或async_wait 等待.
其中wait是同步等待,async_wait是异步等待,需要给一个回调给它。
同一个 io_service 可以同时给多个 timer使下,看下面的示例
#include <iostream>
#include <chrono>
#include <thread>
#include <boost/asio.hpp> #include <boost/asio/high_resolution_timer.hpp >
#include <boost/asio/steady_timer.hpp >
#include <boost/asio/system_timer.hpp > class printer2 {
private:
boost::asio::steady_timer timer_;
int count_;
void print() {
if (count_ < ) {
std::cout << count_ << "\n";
++count_; timer_.expires_from_now(std::chrono::milliseconds ());
timer_.async_wait(std::bind(&printer2::print, this));
}
else
{
std::cout << "Final count is " << count_ << "\n";
delete this;
}
}
printer2(boost::asio::io_service &io)
: timer_(io,std::chrono::milliseconds ()),
count_() {
timer_.async_wait(std::bind(&printer2::print, this)); }
~printer2() { } public: static printer2* Create(boost::asio::io_service &io){
return new printer2(io);
} }; int main() {
boost::asio::io_service io;
printer2::Create(io);
printer2::Create(io);
printer2::Create(io);
printer2::Create(io);
io.run();
std::cin.get();
return ;
}
下面附上我打包好的devcpp 绿色包
DevC++ 5.9.1 with Boost 1.57.0
简单的几个Boost定时器的更多相关文章
- 分享一个简单易用的软件定时器模块(MultiTimer)——基于keil+stm32f103zet+hal库(裸机实现)
公众号上看到一个比较好的一个github项目:https://github.com/0x1abin/MultiTimer 今天看了看,简单的,就移植了- 且看文档的说明, ============== ...
- C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程
创建线程的常用方法:异步委托.定时器.Thread类 理解程序.进程.线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程进程就是在内存中运行的程序(即运行着的程序):一个进程 ...
- boost 定时器.
#include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_service io; b ...
- QML学习笔记(六)- 简单计时器和定时器
做一个简单的qml计时器和定时器,左键触发计时,右键触发定时 GitHub:八至 作者:狐狸家的鱼 本文链接:QML学习笔记(六)- 简单计时器和定时器 左键点击按钮,触发计时器,中键可以暂停计时,同 ...
- jquery定时器的简单代码
当收到消息的时候能够及时的刷新,显示收到消息的条数,下面与大分享下使用简单的代码实现jquery定时器 简单的代码实现jquery定时器. 今天,项目遇到一个消息的模块,在导航条最上面.想实现,当收到 ...
- boost::bind的简单实现
前言 在上一篇blog中简单的实现了boost::function,支持带有2个参数的函数/函数指针,函数对象,函数适配器/bind类,以及带有1个参数的成员函数指针. 本文接着来介绍如何实现一个简单 ...
- boost.ASIO-可能是下一代C++标准的网络库
曾几何时,Boost中有一个Socket库,但后来没有了下文,C++社区一直在翘首盼望一个标准网络库的出现,网络上开源的网络库也有不少,例如Apache Portable Runtime就是比较著名的 ...
- boost.asio系列(一)——deadline_timer
一.构造函数 一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维护多个定时器.在构造deadline_timer时指定时间: basic_deadline_t ...
- Boost.Python简介
Boost.Python简单概括:是Boost库的一部分:用来在C++代码中调用python代码以及在Python代码中调用C++代码,并且避免用户直接操作指针. 以下内容搬运自:https://wi ...
随机推荐
- JVM内部细节之二:偏向锁(Biased Locking)
在前面一片文章<JVM内部细节之一:synchronized关键字及实现细节>中已经提到过偏向锁的概念,在理解什么是偏向锁前必须先理解什么是轻量级锁(Lightweight Locking ...
- Fork/Join编程模型
1.一种并行计算的多线程编程模型 2.开始--任务分割--多线程异步执行---任务合并--阻塞等待合并结果.(分治算法) 3.work-stealing算法: 每个线程维护一个各自的双端的链表,有新任 ...
- 跳表(skiplist)Python实现
# coding=utf-8 # 跳表的Python实现 import random # 最高层数设置为4 MAX_LEVEL = 4 def randomLevel(): ""& ...
- position属性详解
内容: 1.position属性介绍 2.position属性分类 3.relative相对定位 4.absolute绝对定位 5.relative和absolute联合使用进行定位 6.fixed固 ...
- centos7.3给squid搭建代理服务器添加认证apache
证使用浏览器打开 然后输入用户名和密码的方式 所以使用基于web服务的apache的 htpasswd 1 创建用户 设置密码 htpasswd -cd /etc/squid/passwords te ...
- zabbix监控系统日志
监控日志必须让agent运行在主动模式 参考网站:https://www.cnblogs.com/dadonggg/p/8611054.html?from=singlemessage
- CENTOS 挂载ntfs移动硬盘
参考网址: http://www.it610.com/article/3368930.htm (较全)http://blog.51cto.com/ultrasql/1927672
- PHP闭包
# 提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() { }; //带结束符 ...
- python 网页爬虫,下载网络图片
# coding=utf-8 import lxml,bs4,re,requests csvContent='' file = open('D:\\tyc_demo.html','rb') soup ...
- AS3 localToGlobal、globalToLocal方法的总结
(1).localToGlobal (point) 把point看成在(1)内部,计算出该point相当于stage的坐标 (2).globalToLocal (point) point为全局 ...