代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
C++无锁循环缓冲实现
作者:
Jack.arain
/ 发布于
2012/8/3
/
585
C++无锁循环缓冲实现
ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif</div> ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif</div> <div>// 无锁缓冲队列. class circular_buffer { public: circular_buffer(int buf_size) : m_buffer_size(buf_size) , m_circle_buffer(NULL) , m_write_p(0) , m_read_p(0) { m_circle_buffer = new char[m_buffer_size]; } <div> ~circular_buffer() { if (m_circle_buffer) delete[] m_circle_buffer; m_circle_buffer = NULL; } <div> void clear() { m_write_p = 0; m_read_p = 0; } <div> unsigned int size() { return m_buffer_size - (m_write_p - m_read_p); } <div> unsigned int used() { return m_write_p - m_read_p; } <div> unsigned int put_data(char* buffer, unsigned int len) { unsigned int l; len = _min(len, m_buffer_size - m_write_p + m_read_p); /* first put the data starting from fifo->in to buffer end */ l = _min(len, m_buffer_size - (m_write_p & (m_buffer_size - 1))); memcpy(m_circle_buffer + (m_write_p & (m_buffer_size - 1)), buffer, l); /* then put the rest (if any) at the beginning of the buffer */ memcpy(m_circle_buffer, buffer + l, len - l); m_write_p += len; return len; } <div> unsigned int get_data(char* buffer, unsigned int len) { unsigned int l; len = _min(len, m_write_p - m_read_p); /* first get the data from fifo->out until the end of the buffer */ l = _min(len, m_buffer_size - (m_read_p & (m_buffer_size - 1))); memcpy(buffer, m_circle_buffer + (m_read_p & (m_buffer_size - 1)), l); /* then get the rest (if any) from the beginning of the buffer */ memcpy(buffer + l, m_circle_buffer, len - l); m_read_p += len; return len; } <div>protected: inline unsigned int _max(unsigned int a, unsigned int b) { return max(a, b); } <div> inline unsigned int _min(unsigned int a, unsigned int b) { return min(a, b); } <div>private: int m_buffer_size; char* m_circle_buffer; unsigned int m_write_p; unsigned int m_read_p; }; </div>
试试其它关键字
无锁循环
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Jack.arain
贡献的其它代码
(
1
)
.
C++无锁循环缓冲实现
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3