//
// Created by leoxae on 19-9-2.
// #ifndef KEEKOAIROBOT_TIMERTASKHELPER_H
#define KEEKOAIROBOT_TIMERTASKHELPER_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 //KEEKOAIROBOT_TIMERTASKHELPER_H

调用函数()

void timeTask(){
Timer t;
//周期性执行定时任务
// t.StartTimer(1000, std::bind(EchoFunc,"hello world!")); cout << "开始识别==>>>>>" ;
for (int i = 0; i <= 100; i++){
cout << "第" << i << "张识别结果为:" << endl;
// t.StartTimer(1000,std::bind(zbarRec,i));
t.StartTimer(2000,std::bind(rec_NInstr,i));
// t.StartTimer(1000,std::bind(rapidjson3));
std::this_thread::sleep_for(std::chrono::seconds(2));
// std::this_thread::sleep_for(std::chrono::milliseconds(500));
t.Expire();
cout << ">>>>>>>" << endl;
}
// t.StartTimer(250,std::bind(zbarRec,i+1));
// std::this_thread::sleep_for(std::chrono::seconds(1));
// t.Expire();
std::cout << "try to expire timer!" << std::endl; // //周期性执行定时任务
// 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));
}

c++定时器执行任务的更多相关文章

  1. js定时器执行

    第一种:问题请求代表执行打印出来的是什么? //定时器执行页面崩溃 var bo = true; setTimeout(function () { console.log("定时器执行&qu ...

  2. .net MVC全局定时器执行作业

    首先的一个需求是在OA系统中定时跑一些定时作业,例如发放年假等事务,之前的做法是在服务器上加入一个服务,用系统定时作业去跑服务,这样有个问题就是当系统在发布的过程中,有可能忘记启动服务而导致无法定时执 ...

  3. js定时器(执行一次、重复执行)

    代码如下: <script> //定时器 异步运行 function hello(){ alert("hello"); } //使用方法名字执行方法 var t1 = ...

  4. quartz 定时器执行

    类存储job信息 public class JobInfo {//省略setter getter String jobName; String jobGroup; Class<? extends ...

  5. js 定时器 执行一次和重复执行

    1- 执行一次(延时定时器) var t1 = window.setTimeout(function() { console.log('1秒钟之后执行了') },1000) window.clearT ...

  6. Oracle定时器执行多线程

    what里面加下面代码强制执行多线程   begin  execute immediate 'alter session force parallel dml parallel 16';  pkg_s ...

  7. @DisallowConcurrentExecution 注解的作用 【定时器执行完当前任务才开启下一个线程的方式】

    转: @DisallowConcurrentExecution 注解的作用 2018年10月12日 16:42:40 fly_captain 阅读数:4317   Quartz定时任务默认都是并发执行 ...

  8. oracle定时器执行一遍就不执行或本就不执行

    转:http://blog.csdn.net/qq_23311211/article/details/76283689 以sqlplus/ assysdba进入sql命令模式,使用sql:select ...

  9. spring定时器,定时器一次执行两次的问题

    Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...

随机推荐

  1. windows下的_vimrc

    折腾了一天 在https://keelii.github.io/2016/06/13/awsome-window-vimrc/的基础上进行了一些改动 " ------------------ ...

  2. [项目总结]怎么获取TextView行数,为什么TextView获取行数为0?

    1 final TextView textView = new TextView(this); 2 ViewTreeObserver viewTreeObserver = textView.getVi ...

  3. Output of C++ Program | Set 7

    Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...

  4. Android,iOS系统有什么区别

    两者运行机制不同:IOS采用的是沙盒运行机制,安卓采用的是虚拟机运行机制.Android是一种基于Linux的自由及开源的操作系统,iOS是由苹果公司开发的移动操作系统IOS中用于UI指令权限最高,安 ...

  5. live2d

    原文来自https://www.fghrsh.net/post/123.html Live2D 看板娘 v1.4 / Demo 3 - 内置 waifu-tips.json (博客园等网站引用推荐) ...

  6. C# 脚本

    有些情况下,需要在程序运行期间动态执行C#代码,比如,将某些经常改变的算法保存在配置文件中,在运行期间从配置文件中读取并执行运算.这时可以使用C#脚本来完成这些工作. 使用C#脚本需要引用库Micro ...

  7. CF688B Lovely Palindromes 题解

    Content 输入一个数 \(n\),输出第 \(n\) 个偶数位回文数. 数据范围:\(1\leqslant n\leqslant 10^{10^5}\). Solution 一看这吓人的数据范围 ...

  8. 洛谷八月月赛 II T2 题解

    Content 现有 \(T\) 次询问. 每次询问给定一个长度为 \(n\) 的 01 串,记为 \(A\).回答应是一个字符串 \(B\),满足: \(B\) 是长度为 \(m\) 的 01 串. ...

  9. 55 道MySQL基础题

    1.一张表,里面有 ID 自增主键,当 insert 了 17 条记录之后, 删除了第 15, 16, 17 条记录,再把 Mysql 重启,再 insert 一条记 录,这条记录的 ID 是 18 ...

  10. CSS中上下margin的传递和折叠

    CSS中上下margin的传递和折叠 1.上下margin传递 1.1.margin-top传递 为什么会产生上边距传递? 块级元素的顶部线和父元素的顶部线重叠,那么这个块级元素的margin-top ...