C++线程安全的队列是什么
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章将为大家详细讲解有关C++线程安全的队列是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。无界队列#include#include#include#inc
千家信息网最后更新 2025年11月07日C++线程安全的队列是什么
这篇文章将为大家详细讲解有关C++线程安全的队列是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

无界队列
#include#include #include #include #include #include template >class Queue //无界队列{public: Queue() = default; ~Queue() = default; //禁止拷贝和移动,编译器会自动delete /*Queue(const Queue&) = delete; Queue(Queue&&) = delete; Queue& operator=(const Queue&) = delete; Queue& operator=(Queue&&) = delete;*/ void push(const T& val) { emplace(val); } void push(T&& val) { emplace(std::move(val)); } template void emplace(Args&&...args) { std::lock_guard lk{ mtx_ }; q_.push(std::forward (args)...); cv_.notify_one(); } T pop()//阻塞 { std::unique_lock lk{ mtx_ }; cv_.wait(lk, [this] {return !q_.empty(); });//如果队列不为空就继续执行,否则阻塞 assert(!q_.empty()); T ret{ std::move_if_noexcept(q_.front()) }; q_.pop(); return ret; } std::optional try_pop()//非阻塞 { std::unique_lock lk{ mtx_ }; if (q_.empty())return {}; std::optional ret{ std::move_if_noexcept(q_.front()) }; q_.pop(); return ret; } bool empty()const { std::lock_guard lk{ mtx_ }; return q_.empty(); }private: Container q_; mutable std::mutex mtx_; std::condition_variable cv_;};#include int main(){ Queue q; std::thread t1( [&] { for (int i = 0; i < 100; ++i) { q.push(i); } }); std::thread t2( [&] { for (int i = 0; i < 100; ++i) { //std::cout< 有界队列
#include#include #include #include template class Queue{public: Queue(size_t capacity) :q_{ capacity }{} template void push(T&& val)//阻塞 { std::unique_lock lk{ mtx_ }; not_full_.wait(lk, [this] {return !q_.full(); }); assert(!q_.full()); q_.push_back(std::move(std::forward (val))); not_empty_.notify_one(); } template bool try_push(T&& val)//非阻塞 { std::lock_guard lk{ mtx_ }; if (q_.full())return false; q_.push_back(std::forward (val)); not_empty_.notify_one(); return true; } T pop()//阻塞 { std::unique_lock lk{ mtx_ }; not_empty_.wait(lk, [this] {return !q_.empty(); }); asert(!q_.empty()); T ret{ std::move_if_noexcept(q_.front()) }; q_.pop_front(); not_full_.notify_one(); return ret; } std::optional try_pop()//非阻塞 { std::lock_guard lk{ mtx_ }; if (q_.empty())return {}; std::optional ret{ std::move_if_noexcept(q_.front()) }; q_.pop_front(); not_full_.notify_one(); return ret; }private: boost::circular_buffer q_; std::mutex mtx_; std::condition_variable not_full_; std::condition_variable not_empty_;};#include int main(){ Queue q(10); std::thread t1( [&] { for (int i = 0; i < 100; ++i) { q.push(i); } }); std::thread t2( [&] { for (int i = 0; i < 100; ++i) { //std::cout< 关于"C++线程安全的队列是什么"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
阻塞
队列
篇文章
安全
线程
C++
更多
不错
实用
内容
拷贝
文章
知识
编译器
参考
帮助
有关
移动
编译
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发如何制作旋钮开关
涉县软件开发者在线咨询
怎么在网易服务器里用材质包
个人软件开发方法
工行杭州软件开发中心 知乎
软件开发的集成费
网络安全心得150
软件开发如何做需求分析
2018年国家网络安全题库
可以做网络安全网站推荐
制作数据库结构对象数据
交易数据库与网络系统
国泰安数据库值怎么找
夺冠之路数据库
社保管理客户端服务器地址
数据库三级怎么评分的
简述网络安全模型的作用
数据库系统第二章知识点
网络安全培训目的及意义
青少年年网络安全事件
传奇神龙帝国怪物数据库
科学网络技术有限公司
天然气单位网络安全管理制度
武汉大学网络安全学院李淑华
oracle数据库 锁表
战地服务器地址
服务器的用户组管理在哪里
数据库防伪技术量大从优
中金金采网络技术公司
网络安全技术员作文