windows平台一个高性能、通用型的C++生产者/消费者架构模板
生活随笔
收集整理的這篇文章主要介紹了
windows平台一个高性能、通用型的C++生产者/消费者架构模板
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*
生產(chǎn)者/消費者通用模板
特點:
高性能:采用多線程,多隊列平衡的信號量等待模型,有效減少鎖等待
可調(diào)節(jié):可以根據(jù)實際應(yīng)用環(huán)境調(diào)整隊列數(shù),最多可支持64個隊列
使用簡單,一個構(gòu)造函數(shù),一個生產(chǎn)函數(shù),一個消費函數(shù)。
*/
#ifndef PANDC_H
#define PANDC_H#include <vector>
#include <deque>
#include <windows.h>
#include <limits.h>using namespace std;enum QueueType
{qtFIFO,qtFILO
};
//T生產(chǎn)的對象
template<typename t="">
class Pandc
{
public://構(gòu)造函數(shù), QUEUE_COUNT是隊列數(shù) QT:先進先出還是先進后出Pandc(unsigned long QUEUE_COUNT,QueueType QT);~Pandc();void P(T obj); //生產(chǎn)bool C(/*out*/T &ret); //消費long ItemCount(){return m_item_count;}long QueueItemCount(unsigned long queue_id);
private:Pandc(const Pandc&);Pandc& operator=(const Pandc&);
private:volatile LONG m_queue_id; //當(dāng)前隊列ID;vector<deque<t>* > m_queues; //隊列vector<rtl_critical_section*> m_queuelocks; //隊列鎖HANDLE *m_semaphores; //隊列信號量unsigned long m_queue_count; //隊列數(shù)QueueType m_queue_type; //進出隊列方式 FIFO/FILOvolatile LONG m_item_count; //隊列中的總條目數(shù)
};template<typename t="">
long Pandc<t>::QueueItemCount( unsigned long queue_id )
{if (queue_id >= m_queue_count)return 0;return (m_queues[queue_id])->size();
}template<typename t="">
bool Pandc<t>::C(/*out*/T &ret)
{DWORD wait_ret = WaitForMultipleObjects(m_queue_count,m_semaphores,false,INFINITE);if (WAIT_FAILED == wait_ret)return false;size_t i = wait_ret - WAIT_OBJECT_0;EnterCriticalSection(m_queuelocks[i]);if (qtFIFO == m_queue_type){ ret = m_queues[i]->front();m_queues[i]->pop_front();}else{ret = m_queues[i]->back();m_queues[i]->pop_back();}LeaveCriticalSection(m_queuelocks[i]);InterlockedDecrement(&m_item_count);return true;
}template<typename t="">
void Pandc<t>::P( T obj )
{ if (InterlockedIncrement(&m_queue_id) > 1024 * 1024)InterlockedExchange(&m_queue_id,0);size_t i = (m_queue_id % m_queue_count); EnterCriticalSection(m_queuelocks[i]); m_queues[i]->push_back(obj);ReleaseSemaphore(m_semaphores[i],1,NULL);LeaveCriticalSection(m_queuelocks[i]);InterlockedIncrement(&m_item_count);
}template<typename t="">
Pandc<t>::~Pandc()
{for(vector<deque<t> * >::iterator it = m_queues.begin(); it!=m_queues.end();++it){ delete (*it); }for(vector<rtl_critical_section*>::iterator it = m_queuelocks.begin(); it!=m_queuelocks.end();++it){DeleteCriticalSection(*it);delete (*it);}for(size_t i = 0; i<m_queue_count; ++i)="" {="" closehandle(m_semaphores[i]);="" }="" template<typename="" t="">
Pandc<t>::Pandc(unsigned long QUEUE_COUNT,QueueType QT)
{m_queue_id = 0;m_queue_type = QT;m_queue_count = QUEUE_COUNT;m_item_count = 0;m_semaphores = (HANDLE*)malloc(sizeof(HANDLE)*QUEUE_COUNT);memset(m_semaphores,0,sizeof(HANDLE)*QUEUE_COUNT);for(size_t i = 0; i< m_queue_count; ++i){deque<t> *q = new deque<t>;m_queues.push_back(q);RTL_CRITICAL_SECTION *lock = new RTL_CRITICAL_SECTION;InitializeCriticalSection(lock);m_queuelocks.push_back(lock);HANDLE sp = CreateSemaphoreA(NULL,0,LONG_MAX,"");m_semaphores[i] = sp;}
}#endif
總結(jié)
以上是生活随笔為你收集整理的windows平台一个高性能、通用型的C++生产者/消费者架构模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 12个有趣的C语言面试题及答案
- 下一篇: 基于双向链表的增删改查和排序(C++实现