網(wǎng)站首頁 編程語言 正文
無界隊(duì)列
#include#include #include #include #include #include template > class Queue //無界隊(duì)列 { 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(); });//如果隊(duì)列不為空就繼續(xù)執(zhí)行,否則阻塞 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<
有界隊(duì)列
#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<
總結(jié)
原文鏈接:https://blog.csdn.net/Cdreamfly/article/details/123365671
相關(guān)推薦
- 2022-04-08 MariaDB表表達(dá)式之公用表表達(dá)式(CTE)_mariadb
- 2022-02-28 ERROR in Entry module not found: Error: Can't reso
- 2022-09-28 Python變量定義的簡單使用介紹_python
- 2023-11-13 【云原生】python獲取docker stats 容器cpu使用率
- 2022-06-16 Linux中Redis安裝部署的操作步驟_Redis
- 2023-02-10 python自定義函數(shù)中的return和print使用及說明_python
- 2022-06-02 Android超詳細(xì)講解組件LinearLayout的使用_Android
- 2022-09-07 Redis?sentinel哨兵集群的實(shí)現(xiàn)步驟_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支