当前位置: 首页 > news >正文

青岛做网站推广公司哪家好seo博客

青岛做网站推广公司哪家好,seo博客,网站建设从哪入手,空间设计主要做什么定时器要求在固定的时间异步执行一个操作,比如boost库中的boost::asio::deadline_timer,以及MFC中的定时器。也可以利用c11的thread, mutex, condition_variable 来实现一个定时器。 1、使用C11中的thread, mutex, condition_variable来实现一个定时器。…

定时器要求在固定的时间异步执行一个操作,比如boost库中的boost::asio::deadline_timer,以及MFC中的定时器。也可以利用c++11的thread, mutex, condition_variable 来实现一个定时器。

1、使用C++11中的thread, mutex, condition_variable来实现一个定时器。
注:此算法会每一个任务创建一个线程,不推荐。推荐用最下面第2种时间轮算法

#include <iostream>
#include <chrono>
#include <thread>
#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();}void StartTimer(int interval, std::function<void()> task) {if (_expired == false) {return;}_expired = false;std::thread([this, interval, task]() {while (!_try_to_expire) {std::this_thread::sleep_for(std::chrono::milliseconds(interval));task();}{std::lock_guard<std::mutex> locker(_mutex);_expired = true;_expired_cond.notify_one();}}).detach();}void Expire() {if (_expired) {return;}if (_try_to_expire) {return;}_try_to_expire = true;{std::unique_lock<std::mutex> locker(_mutex);_expired_cond.wait(locker, [this] {return _expired == true; });if (_expired == true) {_try_to_expire = false;}}}private:std::atomic<bool> _expired;std::atomic<bool> _try_to_expire;std::mutex _mutex;std::condition_variable _expired_cond;
};int main() {Timer t;t.StartTimer(1000, []() {std::cout << "Hello World!" << std::endl; });std::this_thread::sleep_for(std::chrono::seconds(4));t.Expire();return 0;
}

2、使用时间轮算法:Linux内核就有这个算法。这里也有一个用户态的实现供参考:github.com/facebook/folly。它的高精度版本能实现微妙级别的定时。下面是一个简单的时间轮定时器的C++实现。原文的代码有问题,不能循环定时,经修改已经支持:


#include <chrono>
#include <functional>
#include <list>
#include <mutex>
#include <thread>
#include <vector>class TimerWheel {
public:using Task = std::function<void()>;explicit TimerWheel(size_t wheel_size, int interval_ms): wheel_size_(wheel_size),interval_ms_(interval_ms),wheel_(wheel_size),current_index_(0) {}~TimerWheel() {Stop();}void Start() {if (running_) {return;}running_ = true;thread_ = std::thread([this]() {while (running_) {std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms_));Tick();}std::cout << "timer oooops!" << std::endl;});thread_.detach();}void Stop() {if (!running_) {return;}running_ = false;if (thread_.joinable()) {thread_.join();}}void AddTask(int timeout_ms, Task task) {std::lock_guard<std::mutex> lock(mutex_);size_t ticks = timeout_ms / interval_ms_;size_t index = (current_index_ + ticks) % wheel_size_;size_t allindex = index;for (size_t i = 1 ; allindex < wheel_size_; i++){allindex = index * i;if (allindex >= wheel_size_)break;wheel_[allindex].push_back(task);}}private:void Tick() {std::lock_guard<std::mutex> lock(mutex_);auto& tasks = wheel_[current_index_];for (const auto& task : tasks) {task();}//tasks.clear();current_index_ = (current_index_ + 1) % wheel_size_;}private:size_t wheel_size_;int interval_ms_;std::vector<std::list<Task>> wheel_;size_t current_index_;bool running_ = false;std::thread thread_;std::mutex mutex_;
};

使用方法:
使用static声明以免被析构,可在cpp类外全局声明,第一个参数为任务容器最大数量,第二个参数为定时判断的毫秒数即最低检测时间单位

static TimerWheel timer(10, 1000);

在要使用的地方,启动并添加任务

timer.Start();
timer.AddTask(2000, []() {std::cout << "Task 1" << std::endl; });
timer.AddTask(3000, []() {std::cout << "Task 2" << std::endl; });

可以在需要的时候停止

timer.Stop();

原文链接:https://blog.csdn.net/sinat_28305511/article/details/131495316

http://www.dinnco.com/news/40923.html

相关文章:

  • 泰州网站建设品牌营销成功案例
  • 企业网站建设国内外差异百度链接提交工具
  • php视频网站开发实战推广互联网营销
  • 30岁做网站编辑百度推广一级代理商名单
  • 网站开发的软件有哪些seo诊断方案
  • html网站开发实例视频营销型企业网站诊断
  • 酒店网站的设计摘要微信运营
  • 日本平面设计大师个人网站网站下载
  • 邮箱网站架构网络营销推广目标
  • 保定市做网站公司地址电话杭州搜索引擎推广排名技术
  • 对电子商务网站建设与管理的理解手机网页链接制作
  • 重庆网站开发价格app注册推广平台
  • 二级域名做网站专业搜索引擎seo技术公司
  • 无锡网站制作启航好竞价托管多少钱一个月
  • 开发网站需要多少资金平台软件定制开发
  • 网站建设个人年终总结东莞网站制作的公司
  • 外发加工网磨字海城seo网站排名优化推广
  • 同江佳木斯网站建设seo推广
  • 多用户商城系统网站建设百度店铺
  • 什么是指定网站的域名电子商务沙盘seo关键词
  • 新零售是什么模式湖南靠谱的关键词优化哪家好
  • wordpress 种子插件seo优化排名教程百度技术
  • 升级的网站显示什么泉州seo优化
  • b2b网站怎么做权重网络营销包括哪些
  • 做影视网站推荐哪个服务器外贸网站免费推广b2b
  • 企业网站怎么备案seo教程seo官网优化详细方法
  • 想自己做网站该学些什么外贸网站建站和推广
  • 欧美做愛网站A级百度关键词排名怎么查
  • 对网站做数据分析北京网站建设东轩seo
  • 做网站必须用对方服务器大兴今日头条新闻