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

网站建设类的论文题目国家免费技能培训平台

网站建设类的论文题目,国家免费技能培训平台,东莞网站制作建设,室内设计师网址导航一&#xff1a;概述 本文利用 C 标准库中的多线程、条件变量、互斥锁等工具来实现一个线程安全的队列&#xff0c;并且使用多个线程来向队列中添加和获取数据。 二&#xff1a;实现过程&#xff1a; #include <iostream> #include <queue> #include <mutex&g…

一:概述

        本文利用 C++ 标准库中的多线程、条件变量、互斥锁等工具来实现一个线程安全的队列,并且使用多个线程来向队列中添加和获取数据。 

二:实现过程:

#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <vector>template <typename T>
class ThreadSafeQueue {
public:// 向队列中添加元素void push(const T& value) {std::lock_guard<std::mutex> lock(mutex_);queue_.push(value);cond_var_.notify_one();  // 通知一个等待的线程}// 从队列中取出元素,如果队列为空,阻塞等待T pop() {std::unique_lock<std::mutex> lock(mutex_);cond_var_.wait(lock, [this] { return !queue_.empty(); });  // 等待直到队列非空T value = queue_.front();queue_.pop();return value;}// 判断队列是否为空bool empty() const {std::lock_guard<std::mutex> lock(mutex_);return queue_.empty();}private:mutable std::mutex mutex_;            // 互斥锁,保护队列std::queue<T> queue_;                 // 基础队列std::condition_variable cond_var_;   // 条件变量,用于队列为空时的等待
};// 示例:使用线程安全队列
void producer(ThreadSafeQueue<int>& queue, int numItems) {for (int i = 0; i < numItems; ++i) {queue.push(i);std::cout << "Produced: " << i << std::endl;}
}void consumer(ThreadSafeQueue<int>& queue, int numItems) {for (int i = 0; i < numItems; ++i) {int item = queue.pop();std::cout << "Consumed: " << item << std::endl;}
}int main() {ThreadSafeQueue<int> queue;const int numItems = 10;const int numProducers = 2;const int numConsumers = 2;std::vector<std::thread> threads;// 启动生产者线程for (int i = 0; i < numProducers; ++i) {threads.push_back(std::thread(producer, std::ref(queue), numItems / numProducers));}// 启动消费者线程for (int i = 0; i < numConsumers; ++i) {threads.push_back(std::thread(consumer, std::ref(queue), numItems / numConsumers));}// 等待所有线程完成for (auto& thr : threads) {thr.join();}return 0;
}

http://www.hkea.cn/news/952875/

相关文章:

  • b2b网站开发供应商小程序开发教程全集免费
  • 用自己的手机做网站外链网站是什么
  • 市场调研公司介绍网站推广优化公司
  • 玉溪人民政府网站建设现状新网站seo
  • 湖南餐饮网站建设2023北京封控了
  • 重庆网站设计人员外贸网站搭建推广
  • 局域网内的网站建设西安网站建设公司排名
  • 普通网站报价多少中南建设集团有限公司
  • 蚌埠做网站哪家好全网营销国际系统
  • 沈阳市网站制作谷歌香港google搜索引擎入口
  • 做美食网站的背景高端网站建设制作
  • 文件什么上传到wordpress泉州seo技术
  • 网站地址地图怎么做网页制作的软件有哪些
  • 如何用万网建设网站口碑营销策划方案
  • 做网站的基础架构东莞seo建站公司
  • 嘉兴做网站的哪家好龙岗网站制作
  • 论坛做网站好吗百度官方网页
  • 微信开发者工具获取系统日期seo优化一般包括
  • 怎么用文本做网站百度排行榜风云榜
  • 未来网站开发需求多搜索网站有哪几个
  • 网站建设 成都郑州高端网站制作
  • 快站怎么做淘客网站深圳关键词
  • 做网站时如何去掉网站横条小红书软文案例
  • 图虫南宁百度快速排名优化
  • 上城网站建设app推广文案
  • 网站建设特点宁波seo搜索引擎优化公司
  • 地产商网站建设网球新闻最新消息
  • 做爰全过程网站免费的视频谷歌seo搜索引擎
  • 怎么架设网站seo推广培训
  • 自己网站做问卷调查网页设计学生作业模板