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

网站搬家数据库配置天天外链官网

网站搬家数据库配置,天天外链官网,宣城公司做网站,网站建站案例定时器要求在固定的时间异步执行一个操作#xff0c;比如boost库中的boost::asio::deadline_timer#xff0c;以及MFC中的定时器。也可以利用c11的thread, mutex, condition_variable 来实现一个定时器。 1、使用C11中的thread, mutex, condition_variable来实现一个定时器。…定时器要求在固定的时间异步执行一个操作比如boost库中的boost::asio::deadline_timer以及MFC中的定时器。也可以利用c11的thread, mutex, condition_variable 来实现一个定时器。 1、使用C11中的thread, mutex, condition_variable来实现一个定时器。 注此算法会每一个任务创建一个线程不推荐。推荐用最下面第2种时间轮算法 #include iostream #include chrono #include thread #include mutex #include condition_variableclass 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::functionvoid() 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_guardstd::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_lockstd::mutex locker(_mutex);_expired_cond.wait(locker, [this] {return _expired true; });if (_expired true) {_try_to_expire false;}}}private:std::atomicbool _expired;std::atomicbool _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 vectorclass TimerWheel { public:using Task std::functionvoid();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_guardstd::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_guardstd::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::vectorstd::listTask 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.dnsts.com.cn/news/13605.html

相关文章:

  • 深圳南园网站建设快速申请免费个人网站
  • 南阳市城乡和住房建设局网站seo托管
  • 做网站要不要用控件辽宁省住房和城乡建设厅证件查询
  • 洛阳专业网站设计开发制作建站公司企业网站模板编辑软件
  • 杭州购物网站建设自助打印微信小程序免费制作平台
  • 做网站的的广告词资源搜索引擎搜索神器网
  • 宜都网站建设微信scrm系统
  • 用vs2010做网站论文wordpress 表单 验证
  • 长沙网站建设公司百度平台推广该怎么做
  • 登陆网站密码不保存怎么做wordpress 邮件收发
  • 远程数据库 wordpress淄博网站优化公司
  • 杭州投资公司自适应网站营销型网站推广方式的论文
  • 用人名做网站域名网页设计购物网站模板
  • 玉林住房和城乡建设局网站官网如何制作网页图片素材
  • 外贸网站建设加推广中国十大少儿编程教育品牌
  • 代理商门户网站开发西安seo培训机构
  • 建立网站导航栏的方法安徽国贸集团网站建设
  • 科技类网站风格公众平台注册
  • 网站开发技能网络推广营销服务
  • 贵州网站建设设计公司小众做的好的网站
  • 做博客网站要什么技术成都网站制作实力乐云seo
  • 与设计行业相关的网站wordpress插件源代码
  • 保定公司网站建设南阳哪里做网站比较好
  • 网站的页面风格是什么法治网站的建设整改措施
  • 专门做广东11选5的网站知识库wordpress主题
  • 网站seo方案设计吕梁市城乡建设局网站
  • 档案互动网站建设品质好的办公室装修设计
  • 做网站营销怎么去推广南通市住房和城乡建设厅网站
  • 湖南省城乡住房建设厅网站长沙网红打卡点
  • 小程序模板多少钱博客seo优化技术