網(wǎng)站首頁 編程語言 正文
無界隊列
#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(); });//如果隊列不為空就繼續(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<
有界隊列
#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
相關推薦
- 2022-11-07 C語言字符串函數(shù)模擬實現(xiàn)流程介紹_C 語言
- 2022-09-30 react中使用usestate踩坑及解決_React
- 2024-03-15 Gitea Webhook報錯 webhook.ALLOWED_HOST_LIST setting
- 2022-03-27 帶你從內(nèi)存的角度看Python中的變量_python
- 2022-11-02 Python嵌套函數(shù)與nonlocal使用詳細介紹_python
- 2022-06-07 關于python調(diào)用c++動態(tài)庫dll時的參數(shù)傳遞問題_C 語言
- 2022-12-09 C++?Easylogging++日志庫配置使用超詳細講解_C 語言
- 2022-12-15 Pytorch加載數(shù)據(jù)集的方式總結(jié)及補充_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支