C++ 不用std::queue 实现读取网络字节缓冲去的队列
来源:互联网 发布:色容差计算软件 编辑:程序博客网 时间:2024/06/18 12:14
想必你也和我一样听说了C++ STL 容器的std::queue 貌似有性能问题,内存会持续增加。我并没有深究解决办法,而是暴力自己重写了一个读取字节缓冲区的队列类
废话不多说直接看实现。利用这个这个缓冲机制下一篇将直接实现一个VC++的聊天室的客户端,并贴出源码。
#ifndef PB_CSOCKETQUEUE_H_#define PB_CSOCKETQUEUE_H_#include <iostream>#include <mutex>class PB_CSocketQueue{private:std::mutex m_buff_mutex;struct queue_block{char c;queue_block *next;queue_block(const char &_c){c = _c;next = NULL;}};queue_block *first, *last;int _size;public:PB_CSocketQueue(){first = last = NULL;_size = 0;}~PB_CSocketQueue(){queue_block * p = first;while (p != NULL){queue_block *tmp = p;p = p->next;delete tmp;}}void push(const char& data){if (_size == 0){queue_block * tmp = new queue_block(data);first = last = tmp;}else{queue_block * cur = last;queue_block * tmp = new queue_block(data);last = tmp;cur->next = tmp;}_size++;}bool pop(char& data){if (_size == 0) return false;if (_size == 1){char c = first->c;delete first;first = last = NULL;data = c;}else{queue_block * cur = first->next;char c = first->c;delete first;first = cur;data = c;}_size--;return true;}int indexof(const char& data){int index = 0;queue_block * p = first;while (p != NULL){queue_block *tmp = p;p = p->next;if (data == tmp->c) return index;index++;}return index;}int removeTop(int len){std::lock_guard<std::mutex> locker(m_buff_mutex);char c=0;int count=0;for (size_t i = 0; i < len; i++){if (pop(c) == false) break;count++;}return count;}int copyTo(char * dst, int Len){int count=0;queue_block * p = first;while (p != NULL&&count < Len){queue_block *tmp = p;p = p->next;*dst = tmp->c;count++;dst++;}return count;}void append(char *c, int len){std::lock_guard<std::mutex> locker(m_buff_mutex);for (size_t i = 0; i < len; i++){push(c[i]);}}int length() const{return _size;}};#endif
1 0
- C++ 不用std::queue 实现读取网络字节缓冲去的队列
- 队列(Queue)的C语言实现
- 队列的实现 queue
- queue队列的实现
- C实现消息队列Queue
- 队列(Queue) C 语言实现
- C语言实现队列Queue
- 自定义字节缓冲队列
- C++实现的队列queue
- 队列(Queue ADT)C语言实现
- C语言队列QUEUE简单实现
- C++标准库__std::deque(双端队列),std::queue(队列),std::stack(栈)__由stl的定义我们就可以看出 queue和stack都是基于deque实现的和常用接口
- C++11:基于std::queue和std::mutex构建一个线程安全的队列
- C++/C++11中std::queue的使用
- C++队列模板使用std::queue
- 【day0414 C++】queue 队列的使用
- 用数组去实现队列(c)
- 用数组去实现队列(c)
- 笔记-Hibernate
- BaseActivity的简单封装
- LeetCode上的几道数据库题目
- typedef函数指针用法
- Python爬虫入门系列 笔记一
- C++ 不用std::queue 实现读取网络字节缓冲去的队列
- TCP连接的坑总结
- 后缀表达式与中缀表达式互转的理论知识【转】
- 现代编译原理 第一章
- 1040. Longest Symmetric String (25)
- Fragment实现懒加载以及简单封装
- typedef 函数指针的用法2
- Spring学习笔记-WebServive
- [莫队] [分块] [BZOJ3585] mex