////////////////////////////////////////////////////////////////////////// // 线程安全的queue #ifndef OCC_SHARE_DLL_TEMPLET_HEADER_FILE #define OCC_SHARE_DLL_TEMPLET_HEADER_FILE #include #include #include using namespace std; template class GksQueue { public: std::recursive_mutex m_MutexCS; std::string quename; int m_nCurCount; public: GksQueue() { init(); } ~GksQueue() { Release(); } void SetQueName(std::string name) { std::unique_lock _LOCK(m_MutexCS); quename = name; } bool push(const T &t) { std::unique_lock _LOCK(m_MutexCS); if ( m_queue.size() > m_safeCount ) { printf("%s 队列溢出!!!\n", quename.c_str()); return false; } m_queue.push(t); m_nCurCount++; return true; } int size(void) { std::unique_lock _LOCK(m_MutexCS); int returnnum = (int)m_queue.size(); return returnnum; } int pop(T &t) { //if (!empty()) //{ // int returnnum = 0; // EnterCriticalSection(&m_CS); // returnnum = (int)m_queue.size(); // t = m_queue.front(); // m_queue.pop(); // LeaveCriticalSection(&m_CS); // m_nCurCount = max(0, m_nCurCount - 1); // return returnnum; //} //else //{ // return false; //} //mod:20241013 bool bflag = false; std::unique_lock _LOCK(m_MutexCS); if (!empty()) { t = m_queue.front(); m_queue.pop(); m_nCurCount = max(0, m_nCurCount - 1); bflag = true; } return bflag; } void pop(void) { m_nCurCount = max(0, m_nCurCount - 1); std::unique_lock _LOCK(m_MutexCS); m_queue.pop(); } bool empty(void) { return m_queue.empty(); } void init(void) { m_safeCount = 1000; m_nCurCount = 0; quename = "未知"; } void setSafeCount(int nSC) { m_safeCount = nSC; } void Clear() { std::unique_lock _LOCK(m_MutexCS); //while (!empty()) //{ // m_queue.pop(); //} std::queue empty; swap(empty, m_queue); m_nCurCount = 0; } void Release(void) { Clear(); } /*T front() { EnterCriticalSection(&m_CS); if (!empty()) { return m_queue.front(); } LeaveCriticalSection(&m_CS); }*/ int GetCur_Count() { return m_nCurCount; } private: size_t m_safeCount; public: std::queue m_queue; }; #endif