linux c/c++ 后台开发常用组件之:高性能阻塞队列
来源:互联网 发布:grub2 引导linux 编辑:程序博客网 时间:2024/06/05 18:04
阻塞队列是后台开发中多线程异步架构的基本数据结构,像python, java 都提供线程安全的阻塞队列,c++ 可能需要自己实现一个模板。
从性能考虑,自己没有使用STL的queue作为基本数据结构,而是使用循环数组作为基本数据结构,性能应该比queue高,省去了动态内存分配和回收。
确点就是,队列大小不可动态扩展,当时实际开发中,可以通过压力测试和内存的限制,配置合适的队列大小来满足应用需求。
/********************************************function: thread safe blocking queue.author: liuyidate: 2014.11.13version: 2.0********************************************/#ifndef BLOCK_QUEUE_H#define BLOCK_QUEUE_H#include <iostream>#include <stdlib.h>#include <pthread.h>#include <sys/time.h>using namespace std;template<class T>class block_queue{public:block_queue(int max_size = 1000){if(max_size <= 0){exit(-1);}m_max_size = max_size;m_array = new T[max_size];m_size = 0;m_front = -1;m_back = -1;m_mutex = new pthread_mutex_t;m_cond = new pthread_cond_t;pthread_mutex_init(m_mutex, NULL);pthread_cond_init(m_cond, NULL);}void clear(){pthread_mutex_lock(m_mutex);m_size = 0;m_front = -1;m_back = -1;pthread_mutex_unlock(m_mutex);}~block_queue(){pthread_mutex_lock(m_mutex);if(m_array != NULL)delete m_array;pthread_mutex_unlock(m_mutex);pthread_mutex_destroy(m_mutex);pthread_cond_destroy(m_cond);delete m_mutex;delete m_cond;}bool full()const{pthread_mutex_lock(m_mutex);if(m_size >= m_max_size){pthread_mutex_unlock(m_mutex);return true;}pthread_mutex_unlock(m_mutex);return false;}bool empty()const{pthread_mutex_lock(m_mutex);if(0 == m_size){pthread_mutex_unlock(m_mutex);return true;}pthread_mutex_unlock(m_mutex);return false;}bool front(T& value)const{pthread_mutex_lock(m_mutex);if(0 == m_size){pthread_mutex_unlock(m_mutex);return false;}value = m_array[m_front];pthread_mutex_unlock(m_mutex);return true;}bool back(T& value)const{pthread_mutex_lock(m_mutex);if(0 == m_size){pthread_mutex_unlock(m_mutex);return false;}value = m_array[m_back];pthread_mutex_unlock(m_mutex);return true;}int size()const{int tmp = 0;pthread_mutex_lock(m_mutex);tmp = m_size;pthread_mutex_unlock(m_mutex);return tmp;}int max_size()const{int tmp = 0;pthread_mutex_lock(m_mutex);tmp = m_max_size;pthread_mutex_unlock(m_mutex);return tmp;}bool push(const T& item){pthread_mutex_lock(m_mutex);if(m_size >= m_max_size){pthread_cond_broadcast(m_cond);pthread_mutex_unlock(m_mutex);return false;}m_back = (m_back + 1) % m_max_size;m_array[m_back] = item;m_size++;pthread_cond_broadcast(m_cond);pthread_mutex_unlock(m_mutex);return true;}bool pop(T& item){pthread_mutex_lock(m_mutex);while(m_size <= 0){if(0 != pthread_cond_wait(m_cond, m_mutex)){pthread_mutex_unlock(m_mutex);return false;}}m_front = (m_front + 1) % m_max_size;item = m_array[m_front];m_size--;pthread_mutex_unlock(m_mutex);return true;}bool pop(T& item, int ms_timeout){struct timespec t = {0,0};struct timeval now = {0,0};gettimeofday(&now, NULL);pthread_mutex_lock(m_mutex);if(m_size <= 0){t.tv_sec = now.tv_sec + ms_timeout/1000;t.tv_nsec = (ms_timeout % 1000)*1000;if(0 != pthread_cond_timedwait(m_cond, m_mutex, &t)){pthread_mutex_unlock(m_mutex);return false;}}if(m_size <= 0){pthread_mutex_unlock(m_mutex);return false;}m_front = (m_front + 1) % m_max_size;item = m_array[m_front];m_size--;pthread_mutex_unlock(m_mutex);return true;}private:pthread_mutex_t *m_mutex;pthread_cond_t *m_cond;T *m_array;int m_size;int m_max_size;int m_front;int m_back;};#endif
//测试程序
#include<iostream>#include"block_queue.h"using namespace std;block_queue<int> g_queue(100);void *p(void *args){sleep(1);int data = 0;for(int i = 0; i < 100; i++){g_queue.push(data++);}return NULL;}void *c(void* args){while(true){int t = 0;if(!g_queue.pop(t,1000)){cout<<"timeout"<<endl;continue;}else{cout<<t<<endl;}g_queue.pop(t);cout<<"block="<<t<<endl;}return NULL;}int main(){pthread_t id;pthread_create(&id, NULL, p, NULL);//pthread_create(&id, NULL, p, NULL);//pthread_create(&id, NULL, c, NULL);pthread_create(&id, NULL, c, NULL);for(;;)sleep(1);return 0;}
0 0
- linux c/c++ 后台开发常用组件之:高性能阻塞队列
- linux c/c++ 后台开发常用组件之: 高性能local cache
- c/c++ 后台开发常用组件之:c++日志模块
- linux c/c++ 后台开发常用组件之:c++日志模块
- linux c/c++ 后台开发常用组件之:高级字符串处理库
- linux c/c++ 后台开发常用组件之:c++日志模块
- 高性能阻塞队列
- linux c/c++ 后台开发常用算法之:md5算法c++版实现
- linux c/c++ 后台开发常用算法之:一致性哈希算法
- linux c/c++ 后台开发基础之:c++日志模块
- linux c/c++ 后台开发之—连接池
- linux c/c++ 后台开发之—连接池
- linux c 之sigsuspend 进程阻塞
- 【转】J.U.C之阻塞队列:LinkedTransferQueue
- Linux c 开发 - 常用基础
- 分布式之高性能组件
- Linux 变干边学 学习笔记之C语言开发常用举例
- C语言编程常用数值计算的高性能实现
- Ubuntu 不显示时间了
- js对象创建的方式及其优势和不足7
- DP--Longest Valid Parentheses
- poj3160(Tarjan+Spfa)
- WARNING: Phonon needs QCoreApplication::applicationName to be set to export audio output names throu
- linux c/c++ 后台开发常用组件之:高性能阻塞队列
- 设计模式之装饰模式
- 【DP】 Light OJ 1422 Halloween Costumes
- 【Leetcode】Valid Parentheses in JAVA——一个括号de了20分钟bug的故事
- centos Apache、php、mysql默认安装路径
- 使用栈在O(1)的时间复杂度求最大值
- Crack the Coding Interview[careerup150]正式启动!【倒计时30天】
- I009-黑马程序员_毕向东_Java基础视频教程第01天-09-Hello World的组成部分
- 64位centos6中安装、配置mysql5.6.x的教程