From 28f06bc49a4d8d69f1ea2f767863b7921d12f155 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期六, 08 五月 2021 18:30:48 +0800 Subject: [PATCH] add robust FMutex, works fine; use boost circular. --- src/shm_queue.h | 26 +++++++++++++++++++++++--- 1 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/shm_queue.h b/src/shm_queue.h index c7d3a23..0041f16 100644 --- a/src/shm_queue.h +++ b/src/shm_queue.h @@ -19,15 +19,18 @@ #ifndef SHM_QUEUE_JE0OEUP3 #define SHM_QUEUE_JE0OEUP3 +#include "robust.h" #include "shm.h" #include <atomic> +#include <boost/circular_buffer.hpp> #include <chrono> namespace bhome_shm { template <class D> -using Circular = robust::CircularBuffer<D, Allocator<D>>; +using Circular = boost::circular_buffer<D, Allocator<D>>; +// using Circular = robust::CircularBuffer<D, Allocator<D>>; template <class D> class SharedQueue @@ -49,8 +52,25 @@ } while (steady_clock::now() < end_time); return false; } - bool TryRead(D &d) { return queue_.pop_front(d); } - bool TryWrite(const D &d) { return queue_.push_back(d); } + bool TryRead(D &d) + { + if (!queue_.empty()) { + d = queue_.front(); + queue_.pop_front(); + return true; + } else { + return false; + } + } + bool TryWrite(const D &d) + { + if (!queue_.full()) { + queue_.push_back(d); + return true; + } else { + return false; + } + } private: Circular<D> queue_; -- Gitblit v1.8.0