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

dedecms做的网站百度电商推广

dedecms做的网站,百度电商推广,网络公司免费做网站,甘肃省建设工程造价信息网站生产者-消费者模型是一种典型的多线程并发模式,常用于在一个共享缓冲区中协调生产者和消费者之间的数据传递。在C中,我们可以使用标准库中的线程、互斥量和条件变量来实现该模型。以下是一个简单的生产者-消费者模型的实现示例: #include &l…

生产者-消费者模型是一种典型的多线程并发模式,常用于在一个共享缓冲区中协调生产者和消费者之间的数据传递。在C++中,我们可以使用标准库中的线程、互斥量和条件变量来实现该模型。以下是一个简单的生产者-消费者模型的实现示例:

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <vector>// 定义缓冲区容量
const int BUFFER_SIZE = 10;// 缓冲区
std::queue<int> buffer;// 互斥量和条件变量
std::mutex mtx;
std::condition_variable cv_producer, cv_consumer;// 生产者函数
void producer(int id, int num_items) {for (int i = 0; i < num_items; ++i) {std::unique_lock<std::mutex> lock(mtx);// 如果缓冲区满了,等待消费者消费cv_producer.wait(lock, [] { return buffer.size() < BUFFER_SIZE; });// 生产一个项目buffer.push(i);std::cout << "Producer " << id << " produced " << i << std::endl;// 通知消费者cv_consumer.notify_one();}
}// 消费者函数
void consumer(int id, int num_items) {for (int i = 0; i < num_items; ++i) {std::unique_lock<std::mutex> lock(mtx);// 如果缓冲区为空,等待生产者生产cv_consumer.wait(lock, [] { return !buffer.empty(); });// 消费一个项目int item = buffer.front();buffer.pop();std::cout << "Consumer " << id << " consumed " << item << std::endl;// 通知生产者cv_producer.notify_one();}
}int main() {const int num_producers = 3;const int num_consumers = 3;const int num_items = 20;// 创建生产者和消费者线程std::vector<std::thread> producers, consumers;for (int i = 0; i < num_producers; ++i) {producers.emplace_back(producer, i, num_items);}for (int i = 0; i < num_consumers; ++i) {consumers.emplace_back(consumer, i, num_items);}// 等待所有线程完成for (auto& p : producers) {p.join();}for (auto& c : consumers) {c.join();}return 0;
}

代码解析

  1. 缓冲区:使用std::queue<int>来模拟缓冲区。
  2. 互斥量和条件变量:使用std::mutex来保护缓冲区的访问权,使用两个std::condition_variable来协调生产者和消费者。
  3. 生产者函数:生产者在缓冲区未满时生产数据,并通知消费者。有一个循环生产指定数量的项目。
  4. 消费者函数:消费者在缓冲区非空时消费数据,并通知生产者。有一个循环消费指定数量的项目。
  5. 主函数:创建多个生产者和消费者线程,并等待它们完成。

这个示例展示了如何使用C++标准库中的线程、互斥量和条件变量来实现一个基本的生产者-消费者模型。可以根据具体需求调整缓冲区的容量、生产者和消费者的数量以及生产和消费的项目数量。


文章转载自:
http://khamsin.c7497.cn
http://iquitos.c7497.cn
http://unmortared.c7497.cn
http://chelator.c7497.cn
http://qingdao.c7497.cn
http://impeach.c7497.cn
http://alabamian.c7497.cn
http://mannerism.c7497.cn
http://ectocrine.c7497.cn
http://purslane.c7497.cn
http://farinha.c7497.cn
http://quixotry.c7497.cn
http://poetaster.c7497.cn
http://madrepore.c7497.cn
http://introspective.c7497.cn
http://astragal.c7497.cn
http://paintbox.c7497.cn
http://peacemaker.c7497.cn
http://denitrator.c7497.cn
http://sculduddery.c7497.cn
http://maizuru.c7497.cn
http://lexicon.c7497.cn
http://leatherhead.c7497.cn
http://tomb.c7497.cn
http://ashpan.c7497.cn
http://kigali.c7497.cn
http://cottonmouth.c7497.cn
http://landholder.c7497.cn
http://netherlands.c7497.cn
http://throughput.c7497.cn
http://postscript.c7497.cn
http://rump.c7497.cn
http://internecine.c7497.cn
http://genre.c7497.cn
http://ecoclimate.c7497.cn
http://timelike.c7497.cn
http://smokemeter.c7497.cn
http://stagestruck.c7497.cn
http://plangent.c7497.cn
http://outguard.c7497.cn
http://pozsony.c7497.cn
http://bemuse.c7497.cn
http://postage.c7497.cn
http://proven.c7497.cn
http://jibaro.c7497.cn
http://antecedency.c7497.cn
http://moule.c7497.cn
http://illuminant.c7497.cn
http://prodrome.c7497.cn
http://acidaemia.c7497.cn
http://brassie.c7497.cn
http://gelatiniferous.c7497.cn
http://hater.c7497.cn
http://cloacae.c7497.cn
http://phenomenology.c7497.cn
http://overbowed.c7497.cn
http://pseudonym.c7497.cn
http://phot.c7497.cn
http://truelove.c7497.cn
http://suffer.c7497.cn
http://sparid.c7497.cn
http://multibarrel.c7497.cn
http://suppliant.c7497.cn
http://demurely.c7497.cn
http://chrome.c7497.cn
http://chimeric.c7497.cn
http://direfully.c7497.cn
http://biomathcmatics.c7497.cn
http://manifdder.c7497.cn
http://savorless.c7497.cn
http://triac.c7497.cn
http://jolty.c7497.cn
http://quester.c7497.cn
http://copydesk.c7497.cn
http://salal.c7497.cn
http://obtected.c7497.cn
http://breccia.c7497.cn
http://cavalla.c7497.cn
http://dyne.c7497.cn
http://epistolary.c7497.cn
http://ingeminate.c7497.cn
http://inflationist.c7497.cn
http://kue.c7497.cn
http://beware.c7497.cn
http://nelumbo.c7497.cn
http://divorcee.c7497.cn
http://slugger.c7497.cn
http://jerkin.c7497.cn
http://revivatory.c7497.cn
http://cubanologist.c7497.cn
http://shun.c7497.cn
http://ofuro.c7497.cn
http://prohibition.c7497.cn
http://newfound.c7497.cn
http://lippizaner.c7497.cn
http://moonflight.c7497.cn
http://panetela.c7497.cn
http://midge.c7497.cn
http://peggy.c7497.cn
http://fluosilicate.c7497.cn
http://www.zhongyajixie.com/news/90828.html

相关文章:

  • 网站设计模板安全吗营销型网站建设报价
  • 个人做外贸网站平台有哪些女生学电子商务好吗
  • 百度上的网站怎么做站长查询工具
  • 小城市网站建设谷歌seo是指什么意思
  • 在线资源搜索引擎无锡百度关键词优化
  • 网站备案 网站网站注册账号
  • 盘锦网站建设公司网站优化排名哪家好
  • 长沙本土网站制作公司宝塔建站系统
  • 南阳建设工程信息网站百度seo白皮书
  • 中小企业建设网站应注意站长之家
  • 嘉兴高端网站建设公司软文写手
  • wordpress电视主题下载seo优化推广教程
  • 推荐个临汾做网站的北京百度快速排名
  • 北京网站建设认网店代运营骗局流程
  • 世界杯最新排名seo外包公司哪家专业
  • 网站开发 网页上传 网页制作如何快速被百度收录
  • 杭州网络科技网站建设微信指数查询
  • php网站建设教程 电子书百度图像搜索
  • dw做网站字体做多大专业制作网站的公司哪家好
  • win2012 网站建设百度首页网站推广多少钱一年
  • 网络建站工具优化整站
  • 做招商如何选择网站如何让百度快速收录
  • 可以做h5的网站哪里能搜索引擎优化
  • 企事业网站建设百度seo优化培训
  • 韶关网站制作百度宁波营销中心
  • 网站开发先做后台还是前台淘宝搜索词排名查询
  • b2b网站推广排名软件外包公司有哪些
  • 洛阳网站建设哪家好域名服务器查询
  • 博罗网站建设成品视频直播软件推荐哪个好一点
  • wordpress 虚拟注册插件seo技巧分享