您的位置:首页 > 科技 > 能源 > 威海网站seo_公众号开发者id在哪_站长seo推广_软文发布的平台与板块

威海网站seo_公众号开发者id在哪_站长seo推广_软文发布的平台与板块

2024/12/24 9:15:41 来源:https://blog.csdn.net/weixin_45397344/article/details/144031644  浏览:    关键词:威海网站seo_公众号开发者id在哪_站长seo推广_软文发布的平台与板块
威海网站seo_公众号开发者id在哪_站长seo推广_软文发布的平台与板块

仅支持无参构造 用法后续日志模块中使用

/*
**  File name:   LoopQueue.h
**  Author:      
**  Date:        2024-10-31
**  Brief:       循环队列模板类
**  Note:        
**  Copyright (C) 1392019713@qq.com All rights reserved.
*/#ifndef LOOPQUEUE_H
#define LOOPQUEUE_H#include <vector>
#include <mutex>
#include <condition_variable>
#include <chrono>template<typename T>
class CLoopQueue
{
public:CLoopQueue():m_nMaxSize(0),m_nFront(0),m_nRear(0),m_bDestroy(true),m_nTotalObjects(0),m_loopQueueLock(){}CLoopQueue(const CLoopQueue& rQueue) = delete;CLoopQueue& operator=(const CLoopQueue& rQueue) = delete;virtual ~CLoopQueue(){Destroy();}/** @brief 创建队列* @param nMaxSize 队列最大容量* @return true 创建成功 false 创建失败*/bool Create(int nMaxSize){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);if (nMaxSize <= 0){return false;}if (!m_bDestroy){return true;}for (int i = 0; i < nMaxSize; ++i){T* pObject = OnCreate();if (pObject){m_vecLoopQueue.push_back(pObject);}}m_nMaxSize = nMaxSize;Clear();m_bDestroy = false;return true;}/** @brief 销毁队列* @return*/void Destroy(){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);if (m_bDestroy){return;}for (auto i = 0; i < m_nMaxSize; ++i) {OnDestry(m_vecLoopQueue[i]);}m_vecLoopQueue.clear();m_nMaxSize = 0;m_bDestroy = true;}/** @brief 入队* @param rObject 入队对象* @param nTimeout 超时时间* @param bBlock 是否阻塞* @return true 入队成功 false 入队失败*/bool EnQueue(const T& rObject, int nTimeout = -1, bool bBlock = true) {if (m_bDestroy){return false;}bool bRet = true;std::unique_lock<std::recursive_mutex> lock(m_loopQueueLock);if (bBlock){while (m_nTotalObjects >= m_nMaxSize){if (nTimeout > 0){m_enQueueCond.wait_for(lock, std::chrono::milliseconds(nTimeout));}else {m_enQueueCond.wait(lock);}}}else{if (m_nTotalObjects >= m_nMaxSize && bRet == true){m_nFront = (m_nFront + 1) % m_nMaxSize;m_nTotalObjects--;}}lock.unlock();m_loopQueueLock.lock();if (bRet == true){if (m_nRear >= (int)m_vecLoopQueue.size()) {m_loopQueueLock.unlock();return false;}T* pObject = m_vecLoopQueue[m_nRear];if (!pObject) {m_loopQueueLock.unlock();return false;}*pObject = rObject;m_nRear = (m_nRear + 1) % m_nMaxSize;m_nTotalObjects++;m_deQueueCond.notify_one();}m_loopQueueLock.unlock();return bRet;}/** @brief 出队* @param rObject 出队对象* @param nTimeout 超时时间* @return true 出队成功 false 出队失败*/bool DeQueue(T& rObject, int nTimeout = -1){if (m_bDestroy){return false;}bool bRet = true;std::unique_lock<std::recursive_mutex> lock(m_loopQueueLock);while (m_nTotalObjects <= 0){if (nTimeout > 0){m_deQueueCond.wait_for(m_loopQueueLock, std::chrono::milliseconds(nTimeout));}else{m_deQueueCond.wait(lock);}}lock.unlock();m_loopQueueLock.lock();if (bRet == true){if (m_nFront >= (int)m_vecLoopQueue.size()){m_loopQueueLock.unlock();return false;}T* pObject = m_vecLoopQueue[m_nFront];if (!pObject){m_loopQueueLock.unlock();return false;}rObject = *pObject;m_nFront = (m_nFront + 1) % m_nMaxSize;m_nTotalObjects--;m_enQueueCond.notify_one();}m_loopQueueLock.unlock();return bRet;}/** @brief 清空队列* @return*/void Clear(){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);m_nFront = 0;m_nRear = 0;m_nTotalObjects = 0;}/* * @brief 获取队列大小* @return 队列大小*/int GetSize(){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);if(m_nMaxSize == 0){return 0;}return m_nTotalObjects;}/** @brief 获取队头元素* @return 队头元素*/T* Front(){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);if (GetSize() == 0){return nullptr;}if (m_nFront >= (int)m_vecLoopQueue.size()){return nullptr;}return m_vecLoopQueue[m_nFront];}/** @brief 获取队尾元素* @return 队尾元素*/T* Rear(){std::lock_guard<std::recursive_mutex> lock(m_loopQueueLock);if (GetSize() == 0){return nullptr;}if(m_nRear >= (int)m_vecLoopQueue.size()){return nullptr;}return m_vecLoopQueue[m_nRear];}protected:/** @brief 创建元素* @param pObject 元素指针*/virtual void OnDestry(T* pObject){if (pObject){delete pObject;pObject = nullptr;}}/** @brief 创建元素* @return 元素指针*/virtual T* OnCreate(){return new T;}private:int m_nMaxSize; //最大容量int m_nFront;  //队头int m_nRear; //队尾bool m_bDestroy; //是否销毁int m_nTotalObjects; //元素个数typedef std::vector<T*> LoopQueue;LoopQueue m_vecLoopQueue; //保存队列的容器std::condition_variable_any m_enQueueCond; //入队条件变量std::condition_variable_any m_deQueueCond; //出队条件变量std::recursive_mutex  m_loopQueueLock; //队列的锁 };
#endif // !

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com