1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C++解决方法:多线程同步经典案例之生产者消费者问题

C++解决方法:多线程同步经典案例之生产者消费者问题

时间:2023-12-12 04:21:17

相关推荐

C++解决方法:多线程同步经典案例之生产者消费者问题

后端开发|C#.Net教程

C++生产者消费者

后端开发-C#.Net教程

抄自维基百科 :

qq农场外挂源码,vscode实现3D平移,ubuntu 镜像 zip,tomcat打印输出,爬虫input使用,php 全文检索引擎,怀化关键词seo优化价格,求职网站网页模板下载lzw

本文用一个ItemRepository类表示产品仓库,其中包含一个数组和两个坐标表示的环形队列、一个std::mutex成员、用来保证每次只被一个线程读写操作 (为了保证打印出来的消息是一行一行的,在它空闲的时候也借用的这个互斥量╮(╯▽╰)╭)、两个std::condition_variable表示队列不满和不空的状态,进而保证生产的时候不满,消耗的时候不空。

php后台管理系统源码图片,vscode c语言 编码,ubuntu刷新命令,tomcat jar部署,sqlite3 源开发,为啥要进行古诗文爬虫,格子php,巨鹿整站seo外包,安卓扒网站源码,网页特效跑马灯,jsp用户注册页面模板lzw

#pragma once#include //std::chrono#include //std::mutex,std::unique_lock,std::lock_guard#include //std::thread#include //std::condition_variable#include //std::cout,std::endl#include //std::mapnamespace MyProducerToConsumer { static const int gRepositorySize = 10;//total size of the repository static const int gItemNum = 97;//number of products to produce std::mutex produce_mtx, consume_mtx;//mutex for all the producer thread or consumer thread std::map threadPerformance;//records of every threads producing/consuming number struct ItemRepository {//repository class int m_ItemBuffer[gRepositorySize];//Repository itself (as a circular queue) int m_ProducePos;//rear position of circular queue int m_ConsumePos;//head position of circular queue std::mutex m_mtx;//mutex for operating the repository std::condition_variable m_RepoUnfull;//indicating that this repository is unfull(then producers can produce items) std::condition_variable m_RepoUnempty;//indicating that this repository is unempty(then consumers can produce items) }gItemRepo; void ProduceItem(ItemRepository *ir, int item) { std::unique_lock ulk(ir->m_mtx); while ((ir->m_ProducePos + 1) % gRepositorySize == ir->m_ConsumePos) {//full(spare one slot for indicating) std::cout << "Reposity is full. Waiting for consumers..." <m_RepoUnfull.wait(ulk);//unlocking ulk and waiting for unfull condition } //when unfull ir->m_ItemBuffer[ir->m_ProducePos++] = item;//procude and shift std::cout << "Item No." << item << " produced successfully by " <<std::this_thread::get_id()<<"!" <m_ProducePos == gRepositorySize)//loop ir->m_ProducePos = 0; ir->m_RepoUnempty.notify_all();//item produced, so its unempty; notify all consumers } int ConsumeItem(ItemRepository *ir) { std::unique_lockulk(ir->m_mtx); while (ir->m_ConsumePos == ir->m_ProducePos) {//empty std::cout << "Repository is empty.Waiting for producing..." <m_RepoUnempty.wait(ulk); } int item = ir->m_ItemBuffer[ir->m_ConsumePos++]; std::cout << "Item No." << item << " consumed successfully by " <<std::this_thread::get_id()<<"!" <m_ConsumePos == gRepositorySize) ir->m_ConsumePos = 0; ir->m_RepoUnfull.notify_all();//item consumed, so its unempty; notify all consumers return item; } void ProducerThread() { static int produced = 0;//static variable to indicate the number of produced items while (1) { std::this_thread::sleep_for(std::chrono::milliseconds(10));//sleep long enough in case it runs too fast for other threads to procude std::lock_guardlck(produce_mtx);//auto unlock when break produced++; if (produced > gItemNum)break; gItemRepo.m_mtx.lock(); std::cout << "Producing item No." << produced << "..." << std::endl; gItemRepo.m_mtx.unlock(); ProduceItem(&gItemRepo, produced); } gItemRepo.m_mtx.lock(); std::cout << "Producer thread " << std::this_thread::get_id() << " exited." << std::endl; gItemRepo.m_mtx.unlock(); } void ConsumerThread() { static int consumed = 0; while (1) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::lock_guardlck(consume_mtx); consumed++; if (consumed > gItemNum)break; gItemRepo.m_mtx.lock(); std::cout << "Consuming item available..." << std::endl; gItemRepo.m_mtx.unlock(); ConsumeItem(&gItemRepo); } gItemRepo.m_mtx.lock(); std::cout << "Consumer thread " << std::this_thread::get_id() << " exited." <m_ConsumePos = 0; ir->m_ProducePos = 0; } void Run() { InitItemRepository(&gItemRepo); std::thread thdConsume[11]; std::thread thdProduce[11]; for (auto& t : thdConsume)t = std::thread(ConsumerThread); for (auto& t : thdProduce)t = std::thread(ProducerThread); for (auto& t : thdConsume)t.join(); for (auto& t : thdProduce)t.join(); for (auto& iter : threadPerformance)cout << iter.first << ":" << iter.second << endl; }}

相关文章:

x5音乐网站源码,vscode 折叠代码块,ios远程ubuntu,tomcat弱口令字典,冲洗爬虫,php pdf2swf,南平提供seo服务电话,黑色dj舞曲音乐网站源码,asp读写信息模板lzw

关于java生产者与消费者的实例详解

java多线程之并发协作生产者消费者设计模式

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。