//
// 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. GPU随机采样速度比较

    技术背景 随机采样问题,不仅仅只是一个统计学/离散数学上的概念,其实在工业领域也都有非常重要的应用价值/潜在应用价值,具体应用场景我们这里就不做赘述.本文重点在于在不同平台上的采样速率,至于另外一个重 ...

  2. CVTE第二次笔试

    选择瞎答得,直接编程题目 1. 使用递归将字符串中的数字去并按顺序打印 输入例  adfsafsfs123123eogie09789 输出例 123123 09789 #include<iost ...

  3. 3.0 rust 项目路径

    $ rustc --versionrustc 1.44.0 (49cae5576 2020-06-01) 将代码存在到不同的文件 main.rs mod aa; fn main() { println ...

  4. 数据库系统相关SQL

    杀进程 查出所有被锁住的表 select b.owner TABLEOWNER, b.object_name TABLENAME, c.OSUSER LOCKBY, c.USERNAME LOGINI ...

  5. sftp 上传下载 命令介绍

    sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...

  6. 【Spark】【RDD】初次学习RDD 笔记 汇总

    RDD Author:萌狼蓝天 [哔哩哔哩]萌狼蓝天 [博客]https://mllt.cc [博客园]萌狼蓝天 - 博客园 [微信公众号]mllt9920 [学习交流QQ群]238948804 目录 ...

  7. 【简】题解 AWSL090429 【聚会】

    这题直接换根dp 记录在要转移的点的子树中有多少牛 #include<bits/stdc++.h> using namespace std; #define ll long long #d ...

  8. Linux编译安装、压缩打包、定时任务管理

    编译安装 压缩打包 定时任务管理 一.编译安装 使用源代码,编译打包软件 1.特点 1.可以定制软件 2.按需构建软件 2.编译安装 1.下载源代码包 wget https://nginx.org/d ...

  9. git 基本命令及idea集成使用

    目录 git基本命令使用 设置签名 gitHub 服务配置秘钥 上传代码 更新代码 分支管理 bat脚本更新 idea集成git git基本命令使用 设置签名 签名和秘钥大多数是一起设置的,设置后一般 ...

  10. MM函数(Excel函数集团)

    此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...