c++定时器执行任务
//
// 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++定时器执行任务的更多相关文章
- js定时器执行
第一种:问题请求代表执行打印出来的是什么? //定时器执行页面崩溃 var bo = true; setTimeout(function () { console.log("定时器执行&qu ...
- .net MVC全局定时器执行作业
首先的一个需求是在OA系统中定时跑一些定时作业,例如发放年假等事务,之前的做法是在服务器上加入一个服务,用系统定时作业去跑服务,这样有个问题就是当系统在发布的过程中,有可能忘记启动服务而导致无法定时执 ...
- js定时器(执行一次、重复执行)
代码如下: <script> //定时器 异步运行 function hello(){ alert("hello"); } //使用方法名字执行方法 var t1 = ...
- quartz 定时器执行
类存储job信息 public class JobInfo {//省略setter getter String jobName; String jobGroup; Class<? extends ...
- js 定时器 执行一次和重复执行
1- 执行一次(延时定时器) var t1 = window.setTimeout(function() { console.log('1秒钟之后执行了') },1000) window.clearT ...
- Oracle定时器执行多线程
what里面加下面代码强制执行多线程 begin execute immediate 'alter session force parallel dml parallel 16'; pkg_s ...
- @DisallowConcurrentExecution 注解的作用 【定时器执行完当前任务才开启下一个线程的方式】
转: @DisallowConcurrentExecution 注解的作用 2018年10月12日 16:42:40 fly_captain 阅读数:4317 Quartz定时任务默认都是并发执行 ...
- oracle定时器执行一遍就不执行或本就不执行
转:http://blog.csdn.net/qq_23311211/article/details/76283689 以sqlplus/ assysdba进入sql命令模式,使用sql:select ...
- spring定时器,定时器一次执行两次的问题
Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...
随机推荐
- C语言内自定义汇编函数&调用约定
探究如何在C语言里直接自写汇编函数 裸函数 裸函数与普通函数的区别 普通函数在经过编译器编译时,编译器自动生成保护现场,恢复现场等反汇编代码 当我们想要自己实现函数内部的汇编代码时,就可以告诉汇编器不 ...
- Running shell commands by C++
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; st ...
- android studio 使用 aidl(一)基础用法
最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方 ...
- SpringBoot-RestTemplate测试Controller
1.功能测试类 package com.imooc.controller; import java.io.IOException; import java.math.BigDecimal; impor ...
- Linux基础命令---mail邮件管理程序
mail mail是一个邮件的管理程序,可以用来发送或者接收邮件. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 mail [选项] ...
- [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解
开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...
- vue-cli4脚手架搭建三
组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...
- axios使用步骤详解(附代码)
Axios是一个基于Promise的 HTTP 库,可以用在浏览器和node.js 中,因为尤大大的推荐,axios也变得越来越流行.最近项目中使用axios也遇到了一些问题,就借此机会总结一下,如有 ...
- MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...
- C++内存管理:简易内存池的实现
什么是内存池? 在上一篇 C++内存管理:new / delete 和 cookie中谈到,频繁的调用 malloc 会影响运行效率以及产生额外的 cookie, 而内存池的思想是预先申请一大块内存, ...