如何使用c ++ 11在所需的时间段内执行函数

skf*_*eng 0 c++ multithreading c++11

我想使用c ++ 11来实现类似windows API的功能SetTimer,就像"每隔2秒做一次"

πάν*_*ῥεῖ 5

假设您希望每2秒执行一次此功能

void foo() {
    cout << "Hello from foo()!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

您可以timed_execution使用各种c ++ 11机制提供一个简单的类

struct timed_execution {
    typedef void (*func_type)(void);
    timed_execution(func_type func, const std::chrono::milliseconds period) 
        : func_(func)
        , period_(period)
        , thread_(std::bind(&timed_execution::threadFunc,this))
    {
    }
private:        
    void threadFunc() {
        while(true) {
            std::this_thread::sleep_for(period_);
            func_();
        }
    }
    func_type func_;
    const std::chrono::milliseconds period_;
    std::thread thread_;
};
Run Code Online (Sandbox Code Playgroud)

要以特定周期异步运行该函数,您只需创建此类的实例:

int main() {
    timed_execution t(foo,std::chrono::milliseconds(2000));

    std::this_thread::sleep_for(std::chrono::seconds(60));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请在查看实时样本.


利用模板/可变参数模板提供实际上要执行的函数的参数和返回类型,似乎是一个好主意,改进timed_execution类,并timer像下面一样去一个类:

template<typename CALLBACK_T>
struct timer {

    template<typename D>
    timer(CALLBACK_T func, const D& period) 
        : func_(func)
        , period_(std::chrono::duration_cast<std::chrono::milliseconds>( period ))
        , thread_(std::bind(&timer::threadFunc,this))
    {
    }
private:        
    void threadFunc() {
        while(true) {
            std::this_thread::sleep_for(period_);
            func_();
        }
    }
    CALLBACK_T func_;
    const std::chrono::milliseconds period_;
    std::thread thread_;
};
Run Code Online (Sandbox Code Playgroud)

并有一个单独的make_timer()函数来实例化它

template<typename CALLBACK_T , typename D>
timer<typename std::decay<CALLBACK_T>::type> make_timer( CALLBACK_T&& callback , D&& duration )
{
    return { std::forward<CALLBACK_T>( callback ) , std::forward<D>( duration ) };   
}
Run Code Online (Sandbox Code Playgroud)
int main() {
    auto timer = make_timer(foo,std::chrono::seconds(1));
    auto other = make_timer( [](){ std::cout << "Hello from lambda!\n"; } , std::chrono::seconds{ 1 } );

    std::this_thread::sleep_for(std::chrono::seconds(60));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)