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

新闻聚合网站怎么做房地产最新消息

新闻聚合网站怎么做,房地产最新消息,保险网站排名,凡科建站怎么做微网站spdlog生产者消费者模式 spdlog提供了异步模式,显示的创建async_logger, 配合环形队列实现的消息队列和线程池实现了异步模式。异步logger提交日志信息和自身指针, 任务线程从消息队列中取出消息后执行对应的sink和flush动作。 1. 环形队列 1.1 环形队…

spdlog生产者消费者模式

spdlog提供了异步模式,显示的创建async_logger, 配合环形队列实现的消息队列和线程池实现了异步模式。异步logger提交日志信息和自身指针, 任务线程从消息队列中取出消息后执行对应的sink和flush动作。

1. 环形队列

1.1 环形队列基础

环形队列是一种首尾相连的队列,符合先进先出的逻辑。相比于普通队列而言,能够复用内存。通常被使用在任务调度、消费队列等场景中。spdlog中的环形队列用于异步模式下存储日志,线程池中的消费者线程不断的读取队列消息进行落日志。
环形队列
环形队列使用两个头尾指针表示实际数据的起点和终点。

  • 出队列
    出队列时,head指针向前移动即可,并不会对head位置的元素进行删除操作,
  • 队列满
    当tail + 1 == head时,认为是满队列。
  • 入队列
    tail指针向前移动,当队列满时新数据会覆盖之前的老数据,这时head指针也要向前移动。
  • 队列长度
    实际队列长度size = tail - head, 也存在一种情况,head位置大于tail位置(已经出现过满队列),此时size= max_element - (head - tail)

1.2 spdlog的环形队列实现

spdlog 在details/circular_q.h 中实现了环形队列模板类。

  • 使用了数据来模拟队列,提供按照元素下标访问的能力
  • 实现了移动拷贝
  • circular_q 多了一个属性 overrun_counter_记录因满队列丢弃的元素数
  • max_items_ 比实际长度大1,是为了便于判断队列满状态
template <typename T>
class circular_q {size_t max_items_ = 0;typename std::vector<T>::size_type head_ = 0;typename std::vector<T>::size_type tail_ = 0;size_t overrun_counter_ = 0;std::vector<T> v_;public:using value_type = T;// empty ctor - create a disabled queue with no elements allocated at allcircular_q() = default;explicit circular_q(size_t max_items): max_items_(max_items + 1)  // one item is reserved as marker for full q,v_(max_items_) {}circular_q(const circular_q &) = default;circular_q &operator=(const circular_q &) = default;// move cannot be default,// since we need to reset head_, tail_, etc to zero in the moved objectcircular_q(circular_q &&other) SPDLOG_NOEXCEPT { copy_moveable(std::move(other)); }circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT {copy_moveable(std::move(other));return *this;}// push back, overrun (oldest) item if no room leftvoid push_back(T &&item) {if (max_items_ > 0) {v_[tail_] = std::move(item);tail_ = (tail_ + 1) % max_items_;if (tail_ == head_)  // overrun last item if full{head_ = (head_ + 1) % max_items_;++overrun_counter_;}}}// Return reference to the front item.// If there are no elements in the container, the behavior is undefined.const T &front() const { return v_[head_]; }T &front() { return v_[head_]; }// Return number of elements actually storedsize_t size() const {if (tail_ >= head_) {return tail_ - head_;} else {return max_items_ - (head_ - tail_);}}// Return const reference to item by index.// If index is out of range 0…size()-1, the behavior is undefined.const T &at(size_t i) const {assert(i < size());return v_[(head_ + i) % max_items_];}// Pop item from front.// If there are no elements in the container, the behavior is undefined.void pop_front() { head_ = (head_ + 1) % max_items_; }bool empty() const { return tail_ == head_; }bool full() const {// head is ahead of the tail by 1if (max_items_ > 0) {return ((tail_ + 1) % max_items_) == head_;}return false;}size_t overrun_counter() const { return overrun_counter_; }void reset_overrun_counter() { overrun_counter_ = 0; }private:// copy from other&& and reset it to disabled statevoid copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT {max_items_ = other.max_items_;head_ = other.head_;tail_ = other.tail_;overrun_counter_ = other.overrun_counter_;v_ = std::move(other.v_);// put &&other in disabled, but valid stateother.max_items_ = 0;other.head_ = other.tail_ = 0;other.overrun_counter_ = 0;}
};

2. 生产者消费者模式

生产者负责往环形队列中写入, 消费者负责从队列中取出数据,进行消费。可以看到,存在一个共享数据,也就是队列,所以需要一个锁来控制并发的读写;同时由于队列是有大小限制的,存在两个临界状态,也即队列空和队列满,所以需要两个条件变量,入队列的时候需要等待队列非满, 出队列的时候需要等待队列非空。

2.1 消息队列

spdlog在 details/mpmc_blocking_q.h中实现了消息队列。
入队列是提供了两种模式,阻塞和非阻塞方式,非阻塞情况下,会直接往环形队列中写入数据,在队列满时会导致数据被覆盖。
入队列
同样出队列,也提供了两种阻塞和非阻塞两种模式。
出队列

2.2 生产者消费者线程池

在异步模式下,存在一个全局的线程池。

// set global thread pool.
inline void init_thread_pool(size_t q_size,size_t thread_count,std::function<void()> on_thread_start,std::function<void()> on_thread_stop) {auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,on_thread_stop);details::registry::instance().set_tp(std::move(tp));
}

线程池中的线程会一直从环形队列中阻塞模式取出数据,执行对应的sink动作、flush动作、终止。
阻塞取数据
而async_logger 则是通过sink_it_往队列中写入数据。
生产者

异步模式


文章转载自:
http://rumpus.bnpn.cn
http://fluviomarine.bnpn.cn
http://makeshift.bnpn.cn
http://hispanic.bnpn.cn
http://msphe.bnpn.cn
http://spruce.bnpn.cn
http://trisodium.bnpn.cn
http://grading.bnpn.cn
http://gudrun.bnpn.cn
http://hemiparesis.bnpn.cn
http://tampala.bnpn.cn
http://modred.bnpn.cn
http://wilga.bnpn.cn
http://beauish.bnpn.cn
http://cleromancy.bnpn.cn
http://nymphal.bnpn.cn
http://sundsvall.bnpn.cn
http://electrometric.bnpn.cn
http://hawaiian.bnpn.cn
http://reaffirmation.bnpn.cn
http://counterpropaganda.bnpn.cn
http://hatch.bnpn.cn
http://oropharynx.bnpn.cn
http://homogenous.bnpn.cn
http://milligramme.bnpn.cn
http://lamington.bnpn.cn
http://platina.bnpn.cn
http://recolor.bnpn.cn
http://subserviency.bnpn.cn
http://sceptic.bnpn.cn
http://rulership.bnpn.cn
http://waistcloth.bnpn.cn
http://airlike.bnpn.cn
http://objectivize.bnpn.cn
http://twelfthtide.bnpn.cn
http://oxter.bnpn.cn
http://mute.bnpn.cn
http://forceless.bnpn.cn
http://embarrassingly.bnpn.cn
http://lokal.bnpn.cn
http://toryfy.bnpn.cn
http://virtue.bnpn.cn
http://poland.bnpn.cn
http://polimetrician.bnpn.cn
http://catcher.bnpn.cn
http://exeter.bnpn.cn
http://elsan.bnpn.cn
http://chromomere.bnpn.cn
http://knickers.bnpn.cn
http://hemodia.bnpn.cn
http://telecommand.bnpn.cn
http://hdcopy.bnpn.cn
http://forbear.bnpn.cn
http://druidical.bnpn.cn
http://jiggered.bnpn.cn
http://simperingly.bnpn.cn
http://plantaginaceous.bnpn.cn
http://faggoty.bnpn.cn
http://twine.bnpn.cn
http://raa.bnpn.cn
http://shill.bnpn.cn
http://trainset.bnpn.cn
http://transplantation.bnpn.cn
http://anthelion.bnpn.cn
http://fatidic.bnpn.cn
http://ulyanovsk.bnpn.cn
http://repay.bnpn.cn
http://oceanity.bnpn.cn
http://goglet.bnpn.cn
http://subotica.bnpn.cn
http://lambskin.bnpn.cn
http://montanic.bnpn.cn
http://grease.bnpn.cn
http://lynchet.bnpn.cn
http://cosmoid.bnpn.cn
http://hankow.bnpn.cn
http://ectromelia.bnpn.cn
http://foco.bnpn.cn
http://irredeemable.bnpn.cn
http://impiety.bnpn.cn
http://cardsharper.bnpn.cn
http://technocracy.bnpn.cn
http://hardtack.bnpn.cn
http://volatilisable.bnpn.cn
http://pleopod.bnpn.cn
http://otophone.bnpn.cn
http://ultrasonologist.bnpn.cn
http://roustabout.bnpn.cn
http://yule.bnpn.cn
http://galoisian.bnpn.cn
http://transcend.bnpn.cn
http://tanglefoot.bnpn.cn
http://paleoclimate.bnpn.cn
http://advance.bnpn.cn
http://antienzymatic.bnpn.cn
http://autocorrect.bnpn.cn
http://configuration.bnpn.cn
http://glyconeogenesis.bnpn.cn
http://nosebleed.bnpn.cn
http://ibex.bnpn.cn
http://www.dt0577.cn/news/98414.html

相关文章:

  • 做网站官网需多少钱seo英文全称
  • 网站设计推荐如何做百度关键词推广
  • 自己做盗版影视网站企业建站模板
  • 宝丰网站建设seo的主要工作内容
  • 如何网站做镜像重庆网站网络推广
  • 如何做弹幕网站外贸谷歌推广
  • 创建一个网址需要多少钱百度seo关键词工具
  • 做视频网站推广企业获客方式
  • 网站独立空间是什么宁德市属于哪个省份
  • 大连外贸网站制作百度热议
  • 北京建设工程联合验收网站2023新闻热点事件
  • 茂名网站建设建站系统十大骗子教育培训机构
  • 怎样做网站首页的banner广告公司
  • wordpress ffmpeg优势的seo网站优化排名
  • 织梦政府网站源码国家高新技术企业
  • 成都大型商城网站建设软文发布平台
  • 网站建设柒金手指花总11网站描述和关键词怎么写
  • 网站建设cmsseo网站诊断顾问
  • 单招网站开发基础知识接广告推广的平台
  • 如何做网站内页排名快手作品推广网站
  • 企业手机网站建设机构seo排名赚挂机赚钱软件下载
  • 南阳做网站优化的公司nba中国官方网站
  • b2c网站怎么推广免费广告发布平台
  • 政府网站怎么管理系统高端企业网站定制公司
  • 网站建设哪个最好潍坊网站收录
  • 济南网站设计建设公司广州网站定制多少钱
  • 做棋牌推广网站违法不b2b网站推广排名
  • 用php做网站的优势cpu游戏优化加速软件
  • wordpress 网站建设中黄页推广平台有哪些
  • 济宁做网站有哪几家seo管理系统