From 7d3086a481899b03c230eb06a29aa57677041725 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期三, 08 七月 2020 15:07:51 +0800 Subject: [PATCH] update --- squeue/include/array_lock_free_queue.h | 36 ++++++++++++++++++++++-------------- 1 files changed, 22 insertions(+), 14 deletions(-) diff --git a/squeue/include/array_lock_free_queue.h b/squeue/include/array_lock_free_queue.h index 8bcf579..e03d6cd 100644 --- a/squeue/include/array_lock_free_queue.h +++ b/squeue/include/array_lock_free_queue.h @@ -1,9 +1,9 @@ -#ifndef __LOCK_FREE_QUEUE_IMPL_MULTIPLE_PRODUCER_H__ -#define __LOCK_FREE_QUEUE_IMPL_MULTIPLE_PRODUCER_H__ +#ifndef __ARRAY_LOCK_FREE_QUEUE_IMPL_MULTIPLE_PRODUCER_H__ +#define __ARRAY_LOCK_FREE_QUEUE_IMPL_MULTIPLE_PRODUCER_H__ #include <assert.h> // assert() #include <sched.h> // sched_yield() - +#include "logger_factory.h" /// @brief implementation of an array based lock free queue with support for /// multiple producers @@ -39,6 +39,8 @@ /// @brief calculate the index in the circular array that corresponds /// to a particular "count" value inline uint32_t countToIndex(uint32_t a_count); + + ELEM_T& operator[](unsigned i); private: size_t Q_SIZE; @@ -65,18 +67,14 @@ /// @brief number of elements in the queue std::atomic<uint32_t> m_count; #endif - static int m_reference; - + + private: /// @brief disable copy constructor declaring it private ArrayLockFreeQueue<ELEM_T>(const ArrayLockFreeQueue<ELEM_T> &a_src); - }; - -template <typename ELEM_T> -int ArrayLockFreeQueue<ELEM_T>::m_reference = 0; template <typename ELEM_T> ArrayLockFreeQueue<ELEM_T>::ArrayLockFreeQueue(size_t qsize): @@ -89,7 +87,6 @@ #endif { m_theQueue = (ELEM_T*)mm_malloc(Q_SIZE * sizeof(ELEM_T)); - m_reference++; } @@ -97,10 +94,7 @@ ArrayLockFreeQueue<ELEM_T>::~ArrayLockFreeQueue() { std::cout << "destroy ArrayLockFreeQueue\n"; - m_reference--; - if(m_reference == 0) { - mm_free(m_theQueue); - } + mm_free(m_theQueue); } @@ -312,4 +306,18 @@ return false; } + +template <typename ELEM_T> +ELEM_T& ArrayLockFreeQueue<ELEM_T>::operator[](unsigned int i) +{ + int currentCount = m_count.load(); + uint32_t currentReadIndex = m_readIndex.load(); + if (i < 0 || i >= currentCount) + { + std::cerr << "Error in array limits: " << i << " is out of range\n"; + std::exit(EXIT_FAILURE); + } + return m_theQueue[countToIndex(currentReadIndex+i)]; +} + #endif // __LOCK_FREE_QUEUE_IMPL_MULTIPLE_PRODUCER_H__ -- Gitblit v1.8.0