wangzhengquan
2020-07-08 7d3086a481899b03c230eb06a29aa57677041725
squeue/include/array_lock_free_queue.h
@@ -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;
@@ -304,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__