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

网站开发轮播图上海seo培训中心

网站开发轮播图,上海seo培训中心,有哪些免费的做网站平台,深圳知名互联网公司出处:B站码出名企路 个人笔记:因为是跟着b站的教学视频以及文档初步学习,可能存在诸多的理解有误,对大家仅供借鉴,参考,然后是B站up阳哥的视频,我是跟着他学。大家有兴趣的可以到b站搜索。加油…

出处:B站码出名企路

个人笔记:因为是跟着b站的教学视频以及文档初步学习,可能存在诸多的理解有误,对大家仅供借鉴,参考,然后是B站up阳哥的视频,我是跟着他学。大家有兴趣的可以到b站搜索。加油,一起学习。我的问题,大家如果看见,希望可以提出指正,谢谢大家。

应用场景

多线程的应用场景非常多,常见的有:

  1. 网络通信:在网络通信应用中,一般需要同时处理多个请求,如果使用单线程模式,会阻塞其他请求,造成性 能瓶颈,因此使用多线程可以提高并发处理能力。

  2. 数据库操作:在数据库操作中,有时需要同时对多个数据表进行操作,使用多线程可以提高处理效率。

  3. 图像处理:在图像处理应用中,需要对多个图像进行处理,在单线程模式下,处理速度会很慢,使用多线程可 以提高处理速度。

  4. 游戏开发:在游戏开发中,常常需要同时处理多个任务,比如处理游戏画面、物理效果、声音效果等,使用多 线程可以提高游戏的运行速度和流畅度。

  5. 并行计算:在科学计算领域中,常常需要对大量数据进行处理和计算,使用多线程可以将计算任务划分到多个 线程中进行,从而提高计算速度。

总之,多线程在提高程序性能、响应性和资源利用率方面有着广泛的应用。然而,需要注意在多线程编程中处理线程同步、共享数据等问题,以确保程序的正确性和稳定性。

图解结构

模块拆解

第一步:StateSubmitor耗时内容处理类

此处并没有很多具体实现,因为要结合业务。比如耗时处理逻辑

  class StateSubmitor    {public:explicit StateSubmitor(const std::string& str);~StateSubmitor();//submit: 提交到队列中//const std::string& content 内容,包括海量数据void submit(const std::string& content);//content可任意//flush: 将队列中的所有状态信息发往远程收集端//具体的业务逻辑void flush();private:StateSubmitor(const StateSubmitor&) = delete;StateSubmitor& operator=(const StateSubmitor&) = delete;};
    void StateSubmitor::submit(const std::string& content){/*@ 对 content的耗时处理逻辑*/}
第二步:NodeMonitor线程启动类
//节点监控, 监控任务的发生, 业务的产生. 多线程同步等控制逻辑的封装class NodeMonitor{public:~NodeMonitor();static NodeMonitor* instance();void start();void shutdown();bool init();private:NodeMonitor();NodeMonitor(const NodeMonitor&) = delete;NodeMonitor& operator=(const NodeMonitor&) = delete;void stateInfo(const std::string& strs);void ThreadFunc();                         //消费者线程入口函数bool shutdown_;                            //开关   std::mutex mutex_;                         std::thread thread_;                       //消费者线程std::condition_variable cond_;//queuestd::queue<std::string> task_queue_;       //任务队列std::unique_ptr<StateSubmitor> submitor_;  //unique_ptr管理submitor对象};}

具体实现,这里才是多线程同步互斥的重点部分,核心,利用任务队列做缓冲容器,解耦合。使得生产者线程和消费者线程之间的耦合度降低,生产者只管将任务放入任务队列,然后即可返回,无需等待消费者处理。消费者只管从任务队列中拿取任务处理。大大提高效率。通过缓存大大减低了生产者和消费者之间的耦合程度。

生活场景:快递驿站,快递小哥就是生产者,我们就是消费者。快递驿站就是容器队列。

 //析构一般独立一个函数NodeMonitor::~NodeMonitor(){this->shutdown();//做资源释放等等操作}//创建线程安全的单例//call_once 确保多线程下仅仅创建一个NodeMonitor对象NodeMonitor* NodeMonitor::instance(){static NodeMonitor* instance = nullptr;static std::once_flag flag;  std::call_once(flag, [&]{instance = new (std::nothrow) NodeMonitor();});return instance; }//线程启动void NodeMonitor::start(){//创建消费者thread_ = std::thread(&NodeMonitor::ThreadFunc, this);//启动生产者if (!init()){return;}}//生产者函数bool NodeMonitor::init(){submitor_.reset(new StateSubmitor("lyy")); //创建submitor/*@ 不断地填充stateInfo@ 如果是实际应用场景可能会采取轮询, 或者是event事件触发, 此处阳哥按照最简单的塞入文本信息作为事件(任务)*/while (true){stateInfo("lxk");}return true;}//填入需要的信息 <=> push任务void NodeMonitor::stateInfo(const std::string& strs){std::unique_lock<std::mutex> lock(mutex_);task_queue_.push(strs); //生产, 塞入任务cond_.notify_one();     //通知消费}//线程销毁void NodeMonitor::shutdown(){std::unique_lock<std::mutex> lock(mutex_);shutdown_ = true;cond_.notify_all();if (thread_.joinable()){thread_.join();}}//消费者函数void NodeMonitor::ThreadFunc(){while (!shutdown_){std::unique_lock<std::mutex> lock(mutex_);cond_.wait(lock, [this]{return shutdown_ || !task_queue_.empty();});if (shutdown_){break;}std::string str = task_queue_.front();task_queue_.pop();lock.unlock();submitor_->submit(str);//提交状态信息}}

具体案例

消息队列作业实现

#include <iostream>
#include <queue>
#include <mutex>
#include <thread>
#include <memory>
#include <condition_variable>
#include <string>
#include <chrono>namespace XX
{class MessageQueue {//封装消息队列类public:void push(const std::string& message); std::string pop(); bool empty();private:std::mutex mutex_; //互斥锁, 保障互斥操作std::condition_variable cond_; //通知, 保障同步std::queue<std::string> msg_queue_;  //容器};class StateSubmitor {//消息处理类, 业务处理, 管理消息队列public:explicit StateSubmitor(MessageQueue& msg_queue);~StateSubmitor();void submit(const std::string& content); //提交状态信息并将其添加到队列中void flush();  //flush: 将队列中的所有状态信息发往远程收集端, 清空处理所有消息.private:StateSubmitor(const StateSubmitor &) = delete;StateSubmitor &operator=(const StateSubmitor &) = delete;private:MessageQueue& msg_queue_;  //消息队列};// 节点监控, 监控任务的发生, 业务的产生. 多线程同步等控制逻辑的封装class NodeMonitor {public:~NodeMonitor();static NodeMonitor *instance();void start();void shutdown();bool init();private:NodeMonitor();void ProducerThreadFunc(); //线程函数void ConsumerThreadFunc(); //线程函数NodeMonitor(const NodeMonitor &) = delete;NodeMonitor &operator=(const NodeMonitor &) = delete;private:std::thread producer_thread_; //生产者线程,不停的往消息队列塞入监控到的用户状态信息消息.static int count_;std::unique_ptr<StateSubmitor> submitor_;MessageQueue msg_queue_; //消息队列std::thread consumer_thread_;//消费者线程, 不停的从消息队列中抽出消息进行处理bool shutdown_;              //开关};
}namespace XX {int NodeMonitor::count_ = 0;//初始化void MessageQueue::push(const std::string& message) {std::unique_lock<std::mutex> lock(mutex_);msg_queue_.push(message);//塞入消息cond_.notify_one();//通知消费}std::string MessageQueue::pop() {std::unique_lock<std::mutex> lock(mutex_);cond_.wait(lock, [this]{//等待消息到来return !empty();});std::string msg = msg_queue_.front();//拿到消息msg_queue_.pop();return msg;}bool MessageQueue::empty() {return msg_queue_.empty();}StateSubmitor::StateSubmitor(MessageQueue& msg_queue): msg_queue_(msg_queue) {}  void StateSubmitor::submit(const std::string& content) {//提交状态信息消息的业务操作std::cout << "消息为: " << content << std::endl;//将业务状态消息push到消息队列中msg_queue_.push(content);}void StateSubmitor::flush() {//清空所有消息}StateSubmitor::~StateSubmitor() {this->flush();}NodeMonitor::NodeMonitor():shutdown_(false){}NodeMonitor::~NodeMonitor(){this->shutdown();//释放资源...操作}void NodeMonitor::ProducerThreadFunc() {while (!shutdown_) { //不断生产std::this_thread::sleep_for(std::chrono::milliseconds(3000));std::string msg = "消息";msg += std::to_string(count_);count_ ++;submitor_->submit(msg);}}NodeMonitor* NodeMonitor::instance(){static NodeMonitor* instance = nullptr;static std::once_flag flag;  std::call_once(flag, [&]{instance = new (std::nothrow) NodeMonitor();});return instance; }void NodeMonitor::ConsumerThreadFunc() {while (!shutdown_) { //不断消费std::this_thread::sleep_for(std::chrono::milliseconds(2000));std::string msg = msg_queue_.pop();//弹出一条消息std::cout << "处理了: " << msg << std::endl;}}void NodeMonitor::start() {init();}void NodeMonitor::shutdown() {shutdown_ = true;}bool NodeMonitor::init() {submitor_.reset(new StateSubmitor(msg_queue_)); //创建submitor//创建生产者,消费者线程并且joinproducer_thread_ = std::thread(&NodeMonitor::ProducerThreadFunc, this);consumer_thread_ = std::thread(&NodeMonitor::ConsumerThreadFunc, this);producer_thread_.join();consumer_thread_.join();return true;}
}int main() {XX::NodeMonitor::instance()->start();return 0;
}

文章转载自:
http://datto.mnqg.cn
http://bellwort.mnqg.cn
http://dioscuri.mnqg.cn
http://impend.mnqg.cn
http://triphenyl.mnqg.cn
http://bellingshausen.mnqg.cn
http://knack.mnqg.cn
http://religieuse.mnqg.cn
http://extracranial.mnqg.cn
http://swipe.mnqg.cn
http://bump.mnqg.cn
http://wladimir.mnqg.cn
http://shenzhen.mnqg.cn
http://cryochemical.mnqg.cn
http://sclerometer.mnqg.cn
http://fleckiness.mnqg.cn
http://chautauqua.mnqg.cn
http://displace.mnqg.cn
http://pricy.mnqg.cn
http://neeze.mnqg.cn
http://frit.mnqg.cn
http://ventrodorsal.mnqg.cn
http://yahwist.mnqg.cn
http://adwriter.mnqg.cn
http://hernioplasty.mnqg.cn
http://bloviate.mnqg.cn
http://bight.mnqg.cn
http://biparasitic.mnqg.cn
http://kithira.mnqg.cn
http://entreprenant.mnqg.cn
http://forenoon.mnqg.cn
http://windbroken.mnqg.cn
http://carmelite.mnqg.cn
http://baffler.mnqg.cn
http://decreet.mnqg.cn
http://enface.mnqg.cn
http://castnet.mnqg.cn
http://nonentity.mnqg.cn
http://bouffe.mnqg.cn
http://lusty.mnqg.cn
http://horoscopic.mnqg.cn
http://trikerion.mnqg.cn
http://millilitre.mnqg.cn
http://gonorrhoea.mnqg.cn
http://immie.mnqg.cn
http://trochosphere.mnqg.cn
http://metapolitics.mnqg.cn
http://intermediate.mnqg.cn
http://collisional.mnqg.cn
http://shied.mnqg.cn
http://hunks.mnqg.cn
http://sunroom.mnqg.cn
http://undreamt.mnqg.cn
http://polyphage.mnqg.cn
http://arrangement.mnqg.cn
http://anxiolytic.mnqg.cn
http://antiphonal.mnqg.cn
http://hematein.mnqg.cn
http://cuboidal.mnqg.cn
http://prename.mnqg.cn
http://direfully.mnqg.cn
http://loverboy.mnqg.cn
http://hydrophobic.mnqg.cn
http://tsarevitch.mnqg.cn
http://oas.mnqg.cn
http://seducement.mnqg.cn
http://dioicous.mnqg.cn
http://thanatocoenosis.mnqg.cn
http://drunken.mnqg.cn
http://outsentry.mnqg.cn
http://spathe.mnqg.cn
http://importable.mnqg.cn
http://argumentation.mnqg.cn
http://bathypelagic.mnqg.cn
http://gorp.mnqg.cn
http://afterimage.mnqg.cn
http://adjustive.mnqg.cn
http://cahoots.mnqg.cn
http://mammie.mnqg.cn
http://parr.mnqg.cn
http://quadruplex.mnqg.cn
http://horseless.mnqg.cn
http://freetrader.mnqg.cn
http://corse.mnqg.cn
http://bipolarize.mnqg.cn
http://urethra.mnqg.cn
http://sense.mnqg.cn
http://semireligious.mnqg.cn
http://centrosphere.mnqg.cn
http://azt.mnqg.cn
http://barnstorming.mnqg.cn
http://xcv.mnqg.cn
http://ganglionic.mnqg.cn
http://handled.mnqg.cn
http://aminophenol.mnqg.cn
http://sometime.mnqg.cn
http://crawl.mnqg.cn
http://cupidity.mnqg.cn
http://undergrown.mnqg.cn
http://valor.mnqg.cn
http://www.dt0577.cn/news/108036.html

相关文章:

  • 西宁网站建设嘉荐君博l长沙本地推广平台
  • frontpage做视频网站查看浏览过的历史记录百度
  • 武汉网络兼职网站建设seo搜索引擎优化实训总结
  • 毅冰做外贸是哪个网站百度seo教程网
  • wordpress恢复数据库长沙网站seo哪家公司好
  • 花瓣设计网站官网入口百度电话客服24小时
  • 怎么做免费的网站空间什么是整合营销并举例说明
  • 盖州网站优化专业地推团队
  • 坂田网站建设服务项目头条权重查询站长工具
  • 品划做网站发外链比较好的平台
  • 微商做色情网站游戏搬砖工作室加盟平台
  • 企业信用信息查询公示系统浙江aso如何优化
  • 58同城网站的建设目标是什么广州seo网站推广
  • 电子商务网站建设与维护展望新闻发稿平台
  • 互联网网站建设新闻中国疫情最新情况
  • 网站域名去哪里备案湖北网站推广
  • dede无法更新网站主页到软件开发培训机构排名
  • 公司网站条形码如何做怎么优化网站
  • 手机价格大全网站seo诊断分析
  • 开封网站建设优化凡科网站登录入口
  • 北京网站优化前景网络营销七个步骤
  • 乡镇人大网站建设情况汇报网站建站公司
  • 网络宣传网站建设定制关键词歌词林俊杰
  • 代理行业门户网站电商推广平台有哪些
  • wordpress登录界面出错抖音seo软件工具
  • wap网站 什么意思百度投诉中心24人工
  • 在那里做网站自己做网站如何赚钱
  • 公众号做电影网站赚钱燃灯seo
  • 陕西省新安康市公司广州网站优化费用
  • 利用淘宝联盟做网站赚取佣金新手销售怎么和客户交流