c++定时器执行任务

Posted lx17746071609

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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++定时器执行任务的主要内容,如果未能解决你的问题,请参考以下文章

数据库定时执行sql mysql定时任务 event 执行定时任务 和sql server定时任务 作业执行定时任务

spring定时器启动了但不执行任务,该如何解决

java定时执行任务为啥没有执行

如何用spring实现定时器任务的执行(定时器执行时间可以从数据库获取)

Spring定时任务为啥没有执行

spring定时器任务多任务串行执行问题排查