免费软件下载网站入口,南昌网站建设机构,对比插件 wordpress,企业管理论文出处#xff1a;B站码出名企路
个人笔记#xff1a;因为是跟着b站的教学视频以及文档初步学习#xff0c;可能存在诸多的理解有误#xff0c;对大家仅供借鉴#xff0c;参考#xff0c;然后是B站up阳哥的视频#xff0c;我是跟着他学。大家有兴趣的可以到b站搜索。加油…出处B站码出名企路
个人笔记因为是跟着b站的教学视频以及文档初步学习可能存在诸多的理解有误对大家仅供借鉴参考然后是B站up阳哥的视频我是跟着他学。大家有兴趣的可以到b站搜索。加油一起学习。我的问题大家如果看见希望可以提出指正谢谢大家。
应用场景
多线程的应用场景非常多常见的有 网络通信在网络通信应用中一般需要同时处理多个请求如果使用单线程模式会阻塞其他请求造成性 能瓶颈因此使用多线程可以提高并发处理能力。 数据库操作在数据库操作中有时需要同时对多个数据表进行操作使用多线程可以提高处理效率。 图像处理在图像处理应用中需要对多个图像进行处理在单线程模式下处理速度会很慢使用多线程可 以提高处理速度。 游戏开发在游戏开发中常常需要同时处理多个任务比如处理游戏画面、物理效果、声音效果等使用多 线程可以提高游戏的运行速度和流畅度。 并行计算在科学计算领域中常常需要对大量数据进行处理和计算使用多线程可以将计算任务划分到多个 线程中进行从而提高计算速度。
总之多线程在提高程序性能、响应性和资源利用率方面有着广泛的应用。然而需要注意在多线程编程中处理线程同步、共享数据等问题以确保程序的正确性和稳定性。
图解结构 模块拆解
第一步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::queuestd::string task_queue_; //任务队列std::unique_ptrStateSubmitor 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_lockstd::mutex lock(mutex_);task_queue_.push(strs); //生产, 塞入任务cond_.notify_one(); //通知消费}//线程销毁void NodeMonitor::shutdown(){std::unique_lockstd::mutex lock(mutex_);shutdown_ true;cond_.notify_all();if (thread_.joinable()){thread_.join();}}//消费者函数void NodeMonitor::ThreadFunc(){while (!shutdown_){std::unique_lockstd::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 chrononamespace XX
{class MessageQueue {//封装消息队列类public:void push(const std::string message); std::string pop(); bool empty();private:std::mutex mutex_; //互斥锁, 保障互斥操作std::condition_variable cond_; //通知, 保障同步std::queuestd::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_ptrStateSubmitor 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_lockstd::mutex lock(mutex_);msg_queue_.push(message);//塞入消息cond_.notify_one();//通知消费}std::string MessageQueue::pop() {std::unique_lockstd::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://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.cfpq.cn.gov.cn.cfpq.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.bttph.cn.gov.cn.bttph.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn