多线程队列模型
template <typename T> class Queue { private: std::queue<T> q; pthread_mutex_t q_mutex; pthread_cond_t q_cond; public: Queue() { pthread_mutex_init(&q_mutex, NULL); pthread_cond_init(&q_cond, NULL); } virtual ~Queue() { pthread_mutex_destroy(&q_mutex); pthread_cond_destroy(&q_cond); } // 插入 void insert(const T& e) { pthread_mutex_lock(&q_mutex); q.push(e); pthread_cond_signal(&q_cond); pthread_mutex_unlock(&q_mutex); } // 取数据,队列空则阻塞 void take(T& e) { pthread_mutex_lock(&q_mutex); while(q.empty()) { pthread_cond_wait(&q_cond, &q_mutex) } e = q.front(); q.pop(); pthread_mutex_unlock(&q_mutex); } // 取数据,队列空则返回 bool pull(T& e) { boot ret = false; pthread_mutex_lock(&q_mutex); while(!q.empty()) { e = q.front(); q.pop(); ret = true; } pthread_mutex_unlock(&q_mutex); return ret; } };