简单的几个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 ...
随机推荐
- Docker集群管理(一)—— 基础docker+swarm+shipyard
目的 学习docker的集群管理,摸索出高可用的docker微服务架构方案.本篇文章只初步的了解下swarm(docker新版已集成了swarm)的使用,了解docker的发现服务的基础方法(dock ...
- Procedure-Function oracle
说明:SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML, 数据定义语言DDL,数据控制语言DCL. 0.调试 点击procedure名,右键选择调试.即可进入调试模式.找到procedu ...
- LInux下LD_LIBRARY_PATH的作用与设置
LD_LIBRARY_PATH环境变量主要是用于指定动态链接器(Id)超早elf可执行文件运行时所依赖的动态库(so)的路径,其内容是以冒号分隔的路径列表. Id链接器优先在该变量设置的路径中查找,若 ...
- H5特性回顾
canvas 绘画, video 媒介回放 audio元素 对本地离线存储的更好支持, 新的特殊内容 - 元素 比如 article,footer,header,nav,section, 新的表单控件 ...
- Mybatis知识(3)
1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? ① 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig ...
- mysql创建定时器(event),查看定时器,打开定时器,设置定时器时间
由于项目需要创建定时器(evevt),所以就百度了一下,发现基本都是来源于一个模板,有些功能还不全,现在自己总结一下. 注:mysql版本是从5.1开始才支持event的.如果你的版本低于5.1就先升 ...
- out对象以及网上答题系统
out对象的主要功能是向客户输出响应信息,其主要方法为“print()”,可以输出任意类型的数据,HTML标记可以作为out输出的内容. 代码: 程序截图
- Android中app卡顿原因分析示例
在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. =========== ...
- css样式占位和不占位隐藏元素的方法
不占位隐藏:display:none; 占位隐藏:visibility:hidden;
- makefile 中的赋值
1. 在makefile 中可以使用后面定义的变量,未定义的变量值为空 = 使用变量时执行赋值操作 := 立即执行赋值操作 ?= 如果没有赋值过,就赋予后面的值 += 将后面的值追加到原来的值后面 参 ...