c++11提供了丰富的时间和线程操作函数,比如 std::this_thread::sleep, std::chrono::seconds等。可以利用这些来很方便的实现一个定时器。 
    定时器要求在固定的时间异步执行一个操作,比如boost库中的boost::asio::deadline_timer,以及MFC中的定时器。这里,利用c++11的thread, mutex, condition_variable 来实现一个定时器: 
    定时器要求异步执行任务    ----> 开辟独立的线程 
    定时器要求能够启动和取消 ----> 提供安全的取消操作,使用互斥量和信号量 
    定时器要求每个定时时刻到达的时候执行的任务要尽可能节省时间

实现

#ifndef TIMER_H_
#define TIMER_H_
#include<functional>
#include<chrono>
#include<thread>
#include<atomic>
#include<memory>
#include<mutex>
#include<condition_variable>
class Timer{
public:
Timer() :expired_(true), try_to_expire_(false){
} Timer(const Timer& t){
expired_ = t.expired_.load();
try_to_expire_ = t.try_to_expire_.load();
}
~Timer(){
Expire();
// std::cout << "timer destructed!" << std::endl;
} void StartTimer(int interval, std::function<void()> task){
if (expired_ == false){
// std::cout << "timer is currently running, please expire it first..." << std::endl;
return;
}
expired_ = false;
std::thread([this, interval, task](){
while (!try_to_expire_){
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
task();
}
// std::cout << "stop task..." << std::endl;
{
std::lock_guard<std::mutex> locker(mutex_);
expired_ = true;
expired_cond_.notify_one();
}
}).detach();
} void Expire(){
if (expired_){
return;
} if (try_to_expire_){
// std::cout << "timer is trying to expire, please wait..." << std::endl;
return;
}
try_to_expire_ = true;
{
std::unique_lock<std::mutex> locker(mutex_);
expired_cond_.wait(locker, [this]{return expired_ == true; });
if (expired_ == true){
// std::cout << "timer expired!" << std::endl;
try_to_expire_ = false;
}
}
} template<typename callable, class... arguments>
void SyncWait(int after, callable&& f, arguments&&... args){ std::function<typename std::result_of<callable(arguments...)>::type()> task
(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}
template<typename callable, class... arguments>
void AsyncWait(int after, callable&& f, arguments&&... args){
std::function<typename std::result_of<callable(arguments...)>::type()> task
(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...)); std::thread([after, task](){
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}).detach();
} private:
std::atomic<bool> expired_;
std::atomic<bool> try_to_expire_;
std::mutex mutex_;
std::condition_variable expired_cond_;
};
#endif ////////////////////test.cpp
#include<iostream>
#include<string>
#include<memory>
#include"Timer.hpp"
using namespace std;
void EchoFunc(std::string&& s){
std::cout << "test : " << s << endl;
} int main(){
Timer t;
//周期性执行定时任务
t.StartTimer(1000, std::bind(EchoFunc,"hello world!"));
std::this_thread::sleep_for(std::chrono::seconds(4));
std::cout << "try to expire timer!" << std::endl;
t.Expire(); //周期性执行定时任务
t.StartTimer(1000, std::bind(EchoFunc, "hello c++11!"));
std::this_thread::sleep_for(std::chrono::seconds(4));
std::cout << "try to expire timer!" << std::endl;
t.Expire(); std::this_thread::sleep_for(std::chrono::seconds(2)); //只执行一次定时任务
//同步
t.SyncWait(1000, EchoFunc, "hello world!");
//异步
t.AsyncWait(1000, EchoFunc, "hello c++11!"); std::this_thread::sleep_for(std::chrono::seconds(2)); return 0;
}

c++11实现异步定时器的更多相关文章

  1. boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

    一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待. //makefile #-------------------------------------------------- ...

  2. 2018.2.11 JS的定时器制作

    定时器 1.定时器定义 var time = window.setInterval("执行名词",间隔时间) 关闭定时器 clearInterval(定时器名称) 倒计时定时器 s ...

  3. 使用boost的deadline_timer实现一个异步定时器

    概述 最近在工作上需要用到定时器,然后看到boost里面的deadline_timer可以实现一个定时器,所以就直接将其封装成了ATimer类,方便使用,ATimer有以下优点: 可以支持纳秒.毫秒. ...

  4. C++11 使用异步编程std::async和std::future

    先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...

  5. 2015.3.11 VS异步控件及进度条结合应用

    1.在Form中添加 指针控件:BackgroundWorker-bgwork:进度条控件progressBar1 以及开始.取消按钮 2.开始按钮启动异步线程 private void button ...

  6. 11 Windows编程——定时器

    周期性的发送WWL_TIMER消息的一个东西,这个周期可以由程序员自己设定.设定周期的数是SetTimer,停止定时器消息发送的函数是:Killximer: 定时器消息的特点: 1.不准确(也就是说, ...

  7. 2020.11.2 异步IO 协程

    异步IO 同步IO在一个线程中,CPU执行代码的速度极快,然而,一旦遇到IO操作,如读写文件.发送网络数据时,就需要等待IO操作完成,才能继续进行下一步操作. 在IO操作的过程中,当前线程被挂起,而其 ...

  8. 第11章 Windows线程池(1)_传统的Windows线程池

    第11章 Windows线程池 11.1 传统的Windows线程池及API (1)线程池中的几种底层线程 ①可变数量的长任务线程:WT_EXECUTELONGFUNCTION ②Timer线程:调用 ...

  9. CoreAnimation6-基于定时器的动画和性能调优

    基于定时器的动画 定时帧 动画看起来是用来显示一段连续的运动过程,但实际上当在固定位置上展示像素的时候并不能做到这一点.一般来说这种显示都无法做到连续的移动,能做的仅仅是足够快地展示一系列静态图片,只 ...

随机推荐

  1. hibernate不调用save也保存上了

    List<Instrument> insts = instService.search(search); if (insts.size() == 1) { Instrument inst ...

  2. spring boot 拦截器之WebMvcConfigurerAdapter

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   上一篇我们讲到了拦截器,我们也简单的讲解到了WebMvcConfigurerAdapter这个拦截器.本篇我们来对Web ...

  3. 判断IE浏览器的最简洁方法

    <script type='text/javascript'> var ie = !-[1,]; alert(ie);</script>

  4. e591. Drawing Simple Text

    See also e575 The Quintessential Drawing Program. public void paint(Graphics g) { // Set the desired ...

  5. 标识符的长度应当符合“min-length && max-information”原则

    标识符的长度应当符合“min-length && max-information”原则. 几十年前老 ANSI C 规定名字不准超过 6 个字符,现今的 C++/C 不再有此限制.一 ...

  6. 介绍Unity中相机的投影矩阵与剪切图像、投影概念

    这篇作为上一篇的补充介绍,主要讲Unity里面的投影矩阵的问题: 上篇的链接写给VR手游开发小白的教程:(三)UnityVR插件CardboardSDKForUnity解析(二) 关于Unity中的C ...

  7. 从头认识java-18.2 主要的线程机制(7)join

    这一章节我们来讨论一下join. 1.什么是join? 阻塞当前线程,让.join的线程先做完.然后再继续当前线程 以下是api的解释: A.join,在API中的解释是,阻塞当前线程B,直到A运行完 ...

  8. fatal error: malformed or corrupted AST file: &#39;Unable to load module Darwin.pcm 问题解决

    xcode5 编译project.偶然碰到了以下的问题: fatal error: malformed or corrupted AST file: 'Unable to load module &q ...

  9. 【python】通过代理安装包

    1.安装setuptools 支持 pip install 或easy_install 2.在终端执行 set HTTP_PROXY=http://your.proxy.com:yourPort se ...

  10. 对ChemDraw Prime 16.0你了解多少

    ChemDraw Prime 16.0应用是化学智能绘图程序的行业领导者.除了创建符合出版标准的绘图,化学家们可以使用ChemDraw Prime软件预测性能,搜索数据库等来节省时间,提高数据的准确性 ...