简单的几个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 ...
随机推荐
- TCP阻塞模式开发
在阻塞模式下,在IO操作完成前,执行的操作函数将一直等候而不会立刻返回,该函数所在的进程会阻塞在这里.相反,在非阻塞模式下,套接字函数会立即返回,而不管IO是否完成,该函数所在的线程将继续运行.阻塞模 ...
- ffmpeg 播放音频
播放音频,设置好SDL_AudioSpec播放参数,然后由SDL回调函数进行解码和数据的拷贝,解码播放音频无需设置延迟,因为声卡播放音频是阻塞的 int audio_decode_frame(AVCo ...
- relocation error: /usr/lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference 问题解决
在建立一个错误的软连接到ld-linux-x86-64.so.2时,悲剧就这么发生了.此时大部分命令都不能使用,SSH当然也不能登录了.这个时候一定不要退出终端. 有人说那就把软连接复原吧,可是ln也 ...
- uva297
题意: 一个四叉树用来格式编码图像,这个想法的基础是任意的图像能够分成四个象限.每个象限能够拆分成四个子象限,比如,在四叉树中,每一个图像都被一个父节点表示,根据预先定义好的顺序,四个孩子节点代表四个 ...
- 0_Simple__vectorAdd + 0_Simple__vectorAdd_nvrtc + 0_Simple__vectorAddDrv
▶ 使用 CUDA Runtime API,运行时编译,Driver API 三种接口计算向量加法 ▶ 源代码,CUDA Runtime API #include <stdio.h> #i ...
- CENTOS系统安装及初始化配置相关
一,配置网卡: 1,设置网卡ip地址:vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yes ...
- 10.Action中的method属性
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...
- shell echo 打印换行
echo -e "aaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbb"
- mime设置
ie9对mime有特殊要求,必须要有type才可以. 如果出现css的mime类型不支持.则没有加 type="css/text" 查看本机的mime支持: regedit > ...
- MySQL JSON 类型数据操作
1.Java 中动态扩展字段,会导致数据库表被锁,在MySQL 5.7.8版本之前,因为MySQL不能直接操作JSON类型数据,可以将一个字段设定成varchar类型,里面存放JSON格式数据,这样在 ...