原文地址:https://www.cnblogs.com/gtarcoder/p/4924097.html
c++11提供了丰富的时间和线程操作函数,比如 std::this_thread::sleep, std::chrono::seconds等。可以利用这些来很方便的实现一个定时器。
定时器要求在固定的时间异步执行一个操作,比如boost库中的boost::asio::deadline_timer,以及MFC中的定时器。这里,利用c++11的thread, mutex, condition_variable 来实现一个定时器:
定时器要求异步执行任务 ----> 开辟独立的线程 。
定时器要求能够启动和取消 ----> 提供安全的取消操作,使用互斥量和信号量 。
定时器要求每个定时时刻到达的时候执行的任务要尽可能节省时间。
实现以及示例代码:
#ifndef TIMER_H_
#define TIMER_H_
#include
#include
#include
#include
#include
#include
#include
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 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 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 locker(mutex_);
expired_cond_.wait(locker, [this] {return expired_ == true; });
if (expired_ == true) {
// std::cout << "timer expired!" << std::endl;
try_to_expire_ = false;
}
}
}
template
void SyncWait(int after, callable&& f, arguments&&... args) {
std::function::type()> task
(std::bind(std::forward(f), std::forward(args)...));
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}
template
void AsyncWait(int after, callable&& f, arguments&&... args) {
std::function::type()> task
(std::bind(std::forward(f), std::forward(args)...));
std::thread([after, task]() {
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}).detach();
}
private:
std::atomic expired_;
std::atomic try_to_expire_;
std::mutex mutex_;
std::condition_variable expired_cond_;
};
#endif
#include
#include
#include
#include"Timer.h"
using namespace std;
void EchoFunc(std::string&& s) {
std::cout << "test : " << s << endl;
}
void Add(int a, int b)
{
std::cout << "a=" << a << " " << "b=" << b <<" "<<"a+b="<