Professional Documents
Culture Documents
Queue: Queue - Mutex Queue - Cond Dequeuecdr Handlecdr
Queue: Queue - Mutex Queue - Cond Dequeuecdr Handlecdr
Queue: Queue - Mutex Queue - Cond Dequeuecdr Handlecdr
//pthread_mutex_t queue_mutex; pthread_cond_t queue_cond; void dequeueCdr (sessionCdr **); void handleCdr (sessionCdr *); public: CdrSession (); //static void *cdrSessionthread(void *arg); bool initialize (); void cdrManager (); void enqueueCdr (sessionCdr **); }; void *cdrSessionthread (void *arg); extern CdrSession *CdrSessionMgr;
#ifndef __SYNC_MSG_QUEUE_ #define __SYNC_MSG_QUEUE_ #include <pthread.h> #include <deque> template < typename T > class SyncMsgQueue { public: SyncMsgQueue () { pthread_mutex_init (&m_theMutex, NULL); pthread_cond_init (&m_theCondition, NULL); } ~SyncMsgQueue () { pthread_mutex_destroy (&m_theMutex); pthread_cond_destroy (&m_theCondition); } void push (T msg) { pthread_mutex_lock (&m_theMutex); if (!m_theQueue.empty ()) { m_theQueue.push_back (msg);
} else { m_theQueue.push_back (msg); pthread_cond_broadcast (&m_theCondition); } pthread_mutex_unlock (&m_theMutex); } void push_N (std::deque < T > &pQueue) { pthread_mutex_lock (&m_theMutex); if (!m_theQueue.empty ()) { m_theQueue.insert (m_theQueue.end (), pQueue.begin (), pQueue.end ()); } else { m_theQueue.insert (m_theQueue.end (), pQueue.begin (), pQueue.end ()); pthread_cond_broadcast (&m_theCondition); } pthread_mutex_unlock (&m_theMutex); } T pop () { T msg; pthread_mutex_lock (&m_theMutex); while (m_theQueue.empty ()) { pthread_cond_wait (&m_theCondition, &m_theMutex); } msg = m_theQueue.front (); m_theQueue.pop_front (); pthread_mutex_unlock (&m_theMutex); return msg; } private: std::deque < T > m_theQueue; pthread_mutex_t m_theMutex; pthread_cond_t m_theCondition; }; #endif