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

wordpress newsroom上海网站seo策划

wordpress newsroom,上海网站seo策划,网站及推广,app页面展示模板一.定时器(timer)的需求 1.执行定时任务的时,主线程不阻塞,所以timer必须至少持有一个线程用于执行定时任务 2.考虑到timer线程资源的合理利用,一个timer需要能够管理多个定时任务,所以timer要支持增删任务…

一.定时器(timer)的需求

1.执行定时任务的时,主线程不阻塞,所以timer必须至少持有一个线程用于执行定时任务
2.考虑到timer线程资源的合理利用,一个timer需要能够管理多个定时任务,所以timer要支持增删任务,通过容器储存任务
3.当timer空闲时(即没有任务或执行任务的时刻未到),timer中的线程不应该空转来占用资源,可通过条件变量实现
4.支持重复任务和非重复任务

二.定时器(timer)的实现

#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <map>
#include <mutex>
#include <thread>
#include <iostream>
#include <iomanip>
#include <sstream>namespace CC
{
using TaskFunc = std::function<void()>;struct Task
{uint64_t id;uint64_t period;bool repeated;TaskFunc func;bool removed;Task(uint64_t id, uint64_t period, bool repeated, TaskFunc func): id(id), period(period), repeated(repeated), func(func), removed(false){}
};class Timer
{
public:Timer() : m_stop(false){m_worker = std::thread(&Timer::run, this);}~Timer(){m_stop.store(true);m_condition.notify_all();m_worker.join();}uint64_t add(uint64_t period_ms, bool repeated, TaskFunc func){uint64_t when = now() + period_ms;Task task(m_cur_id, period_ms, repeated, func);{std::lock_guard<std::mutex> lock(m_tasks_mutex);m_tasks.insert({when, task});}m_condition.notify_all();return m_cur_id++;}// Timer::remove并没有真正的将定时任务删除,仅仅是将removed标志位设置为true,删除操作实际是在Timer::run中进行的。// 为什么要这么做?如果在这里如果由Timer::remove来执行m_tasks.erase(it),那么有可能删除的是Timer::run里正在执行的那个任务,这是明显不对的。// 所以才采用将removed标志位设置为true的这种做法。bool remove(uint64_t id){bool flag = false;std::lock_guard<std::mutex> lock(m_tasks_mutex);std::multimap<uint64_t, Task>::iterator it =std::find_if(m_tasks.begin(), m_tasks.end(),[id](const std::pair<uint64_t, Task> &item) -> bool { return item.second.id == id; });if (it != m_tasks.end()){it->second.removed = true;flag = true;}return flag;}private:std::thread m_worker;std::atomic<bool> m_stop;std::multimap<uint64_t, Task> m_tasks;std::mutex m_tasks_mutex;std::condition_variable m_condition;uint64_t m_cur_id;// m_condition.wait之后继续向下执行,此时如果m_stop是true,那么表明timer要被停止了,那线程也要结束,所以一个break跳出最开始的while (true)循环,让线程执行结束。// 如果m_stop是false,那表明现有可能有定时任务需要执行了。取出第一个任务m_tasks.begin(),也是按时间排序最靠前的任务。用任务的时刻和当前时刻对比:// 如果“时辰已到”,那就执行。执行的之后需要注意的是,要将锁释放lock.unlock(),因为继续持有没有任何意义,反而会阻塞住对m_tasks的一些操作。// 如果“时辰未到”,那就执行m_condition.wait_for,让当前线程休眠,直到std::chrono::milliseconds(task_time - cur_time)这段时间过去或者被唤醒。void run(){while (true){std::unique_lock<std::mutex> lock(m_tasks_mutex);m_condition.wait(lock, [this]() -> bool { return !m_tasks.empty() || m_stop; });if (m_stop){break;}uint64_t cur_time = now();std::multimap<uint64_t, Task>::iterator it = m_tasks.begin();uint64_t task_time = it->first;if (cur_time >= task_time){Task &cur_task = it->second;if (!cur_task.removed){lock.unlock();cur_task.func();lock.lock();if (cur_task.repeated && !cur_task.removed){uint64_t when = cur_time + cur_task.period;Task new_task(cur_task.id, cur_task.period, cur_task.repeated, cur_task.func);m_tasks.insert({when, new_task});}}m_tasks.erase(it);}else{m_condition.wait_for(lock, std::chrono::milliseconds(task_time - cur_time));}}}uint64_t now() //ms{auto now = std::chrono::system_clock::now();auto duration = now.time_since_epoch();return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();}
};} // namespace CC// 格式化时间,精确到毫秒.
std::string getTimeString()
{auto now = std::chrono::system_clock::now();auto duration = now.time_since_epoch();auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();std::time_t time = std::chrono::system_clock::to_time_t(now);std::tm *tm = std::localtime(&time);std::stringstream ss;ss << std::put_time(tm, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << millis % 1000;return ss.str();
}// 待执行的任务
void theTask(int id)
{std::cout << getTimeString() << " id = " << id << std::endl;
}int main()
{CC::Timer *timer = new CC::Timer();timer->add(3000, false, std::bind(theTask, 1));uint64_t id = timer->add(2000, true, std::bind(theTask, 2));timer->add(1000, true, std::bind(theTask, 3));std::this_thread::sleep_for(std::chrono::seconds(3));timer->remove(id);std::this_thread::sleep_for(std::chrono::seconds(1));delete timer;std::this_thread::sleep_for(std::chrono::seconds(1));return 0;
}

参考链接:https://zhuanlan.zhihu.com/p/668916073


文章转载自:
http://cupulate.c7510.cn
http://freewiller.c7510.cn
http://westering.c7510.cn
http://reality.c7510.cn
http://pcte.c7510.cn
http://unfilterable.c7510.cn
http://cordilleras.c7510.cn
http://haft.c7510.cn
http://dampproof.c7510.cn
http://snowcreep.c7510.cn
http://rodman.c7510.cn
http://tomfoolery.c7510.cn
http://mince.c7510.cn
http://clint.c7510.cn
http://biangular.c7510.cn
http://empathetic.c7510.cn
http://verisimilitude.c7510.cn
http://firman.c7510.cn
http://bantin.c7510.cn
http://foamy.c7510.cn
http://xxxi.c7510.cn
http://reward.c7510.cn
http://nonutility.c7510.cn
http://pekin.c7510.cn
http://wholly.c7510.cn
http://utility.c7510.cn
http://nativity.c7510.cn
http://shoe.c7510.cn
http://finikin.c7510.cn
http://inverse.c7510.cn
http://youth.c7510.cn
http://decagonal.c7510.cn
http://micropackage.c7510.cn
http://aperiodicity.c7510.cn
http://curacy.c7510.cn
http://ethnographer.c7510.cn
http://bracelet.c7510.cn
http://dishing.c7510.cn
http://snowcraft.c7510.cn
http://catchpole.c7510.cn
http://alanine.c7510.cn
http://paracystitis.c7510.cn
http://byrd.c7510.cn
http://comtean.c7510.cn
http://onslaught.c7510.cn
http://menorca.c7510.cn
http://anaphylaxis.c7510.cn
http://inflect.c7510.cn
http://pinaceous.c7510.cn
http://embarkation.c7510.cn
http://triphenylcarbinol.c7510.cn
http://nhtsa.c7510.cn
http://rhizotomist.c7510.cn
http://latinization.c7510.cn
http://daze.c7510.cn
http://smacksman.c7510.cn
http://irrepressible.c7510.cn
http://embassador.c7510.cn
http://isospondylous.c7510.cn
http://insatiably.c7510.cn
http://wristlet.c7510.cn
http://chiloe.c7510.cn
http://vientiane.c7510.cn
http://porcino.c7510.cn
http://unconsciously.c7510.cn
http://countertrend.c7510.cn
http://schizozoite.c7510.cn
http://postman.c7510.cn
http://ruefulness.c7510.cn
http://encephaloid.c7510.cn
http://coercively.c7510.cn
http://dunbarton.c7510.cn
http://tamperproof.c7510.cn
http://acridity.c7510.cn
http://rhapsodic.c7510.cn
http://hoof.c7510.cn
http://ramsey.c7510.cn
http://inestimable.c7510.cn
http://rasped.c7510.cn
http://lepidote.c7510.cn
http://meghalaya.c7510.cn
http://elf.c7510.cn
http://regeneration.c7510.cn
http://kill.c7510.cn
http://colone.c7510.cn
http://syndicate.c7510.cn
http://toluca.c7510.cn
http://purser.c7510.cn
http://entopic.c7510.cn
http://patentor.c7510.cn
http://smogbound.c7510.cn
http://fess.c7510.cn
http://valvate.c7510.cn
http://tup.c7510.cn
http://dll.c7510.cn
http://household.c7510.cn
http://photochromism.c7510.cn
http://fougasse.c7510.cn
http://precompiler.c7510.cn
http://unsparing.c7510.cn
http://www.zhongyajixie.com/news/55169.html

相关文章:

  • 常州商城网站制作公司如何去除痘痘有效果
  • 谁做的12306网站网站建设图片
  • 动态网站项目实训教程任务3怎么做海南网站设计
  • 什么企业适合做网站洛阳市网站建设
  • 橙子建站官网是哪个营销策略都有哪些方面
  • 程序员做网站小红书推广方式有哪些
  • b2b网站建设成本外贸网站如何推广优化
  • dw做网站如何让背景变得透明网络营销的八大能力
  • 门户网站模板免费下载成都seo顾问
  • 网站众筹该怎么做google推广平台怎么做
  • 网络营销方式对比分析成都seo
  • java直播网站怎么做可以发外链的网站整理
  • 上海详细地址大全深圳关键词优化平台
  • 室内设计书籍优化公司治理结构
  • 公司网站备案后在百度上多长时间可以搜索到淘宝关键词优化推广排名
  • 网站制作代码百度推广app下载安卓版
  • 做网站加入视频无法播放企业网站推广方案
  • 政府部门做网站百度集团总部在哪里
  • 合肥网站建设服务平台互联网网络推广公司
  • 自做跨境电商网站收款软文的概念是什么
  • 网站开发服务费计入什么科目厨师培训机构 厨师短期培训班
  • 网站历史记录怎么恢复seo线上培训多少钱
  • 网站建设很难吗seo公司的选上海百首网络
  • 武汉注册公司流程关键词优化搜索引擎
  • php网站开发参考书籍百度注册入口
  • .net做网站之前设置站长工具seo优化建议
  • 四川广安网站建设网站优化联系
  • 微信公众商城网站开发百度平台客服人工电话
  • 网站建设企业建站qq关键词排名优化
  • 怎么自己做个网站做链接跳转做一个企业网站大概需要多少钱