no message

master
zcy 2023-05-21 22:55:13 +08:00
parent 298480b9f1
commit 3b3a50a07c
4 changed files with 30 additions and 15 deletions

View File

@ -10,7 +10,8 @@
"thread": "cpp",
"vector": "cpp",
"xmemory": "cpp",
"xtree": "cpp"
"xtree": "cpp",
"ostream": "cpp"
},
"cmake.buildDirectory": "${workspaceFolder}/build",
"cmake.mingwSearchDirs": [

View File

@ -1,13 +1,13 @@
#ifndef __RINGBUFFER__
#define __RINGBUFFER__
#include <memory>
#include<stdint.h>
#include <mutex>
template <typename T> class RingBuffer{
public:
T At(uint64_t pos);
RingBuffer(uint64_t size);
RingBuffer(uint64_t size,bool ts);
int Add(T *data,uint64_t len);
int TakeBack(T *data,uint64_t len);
int TakeFront(T *data,uint64_t len);
@ -22,15 +22,17 @@ public:
~RingBuffer();
protected:
bool m_threadsafe;
T *m_data;
T *m_data_end;
uint64_t m_size; // 容量
uint32_t m_len; // 数据长度
T *m_head;
T *m_head; // 数组头
std::mutex m_mutex; // 互斥锁
};
template<typename T>
RingBuffer<T>::RingBuffer(uint64_t size) :
RingBuffer<T>::RingBuffer(uint64_t size,bool ts) :
m_data(nullptr),
m_size(0),
m_head(0)
@ -40,6 +42,7 @@ RingBuffer<T>::RingBuffer(uint64_t size) :
m_data_end = &m_data[size - 1];
m_head = m_data;
m_len = 0;
m_threadsafe = ts;
}
template<typename T>
@ -103,6 +106,8 @@ T RingBuffer<T>::At(uint64_t pos){
if(p > m_data_end){
p = p - m_size;
}
// printf("\r\n at %u: %d\r\n",p,*p);
return *p;
}
@ -113,12 +118,20 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
}
if(m_len == m_size)
return 0;
int left = m_size - m_len;
if(len > left){
len = left;
}
T *tail = nullptr;
// like 1023 0 1 2 or 0 1 2 3
if((m_head + m_len) > m_data_end){
tail = m_head + m_len - m_size;
}else{
tail = m_head + m_len;
}
printf("head %u len %u tail %u\r\n",m_head,len,tail);
int i = 0;
for(i = 0;i < len;i++){
T* tmp = tail;
@ -129,12 +142,13 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
tmp = m_data;
}
if(tmp == m_head) {
(*tail) = data[i];
printf("break %u %u\r\n",m_head,tmp);
i++;
break;
}
(*tail) = data[i];
tail = tmp;
// printf("add %u:%d\r\n",tail,data[i]);
}
m_len += i;
return i;
@ -151,17 +165,16 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
if(m_len == 0)
return 0;
if((m_data + (m_len - 1)) > m_data_end){
tail = m_data + (m_len - 1) - m_size;
if((m_head + (m_len - 1)) > m_data_end){
tail = m_head + (m_len - 1) - m_size;
printf("m_data_end:%u m_head + m_len:%u m_head: %u m_len:%ld tail: %u m_size: %ld m_data:%u\r\n",m_data_end,(m_head + m_len),m_head,m_len,tail,m_size,m_data);
}else{
tail = m_data + m_len - 1;
tail = m_head + m_len - 1;
}
int i = 0;
for(i = 0;i < len;i++) {
// std::cout<<"take" <<i<<std::endl;
data[len - 1 - i] = (*tail) ;
if((tail == m_head)) {
i++;
@ -185,11 +198,10 @@ int RingBuffer<T>::TakeFront(T *data,uint64_t len){
T *tail = nullptr;
if(m_len == 0)
return 0;
if((m_head + m_len) > m_data_end){
tail = m_head + m_len - m_size;
}else{
tail = m_head + m_len - 1;
if(len > m_len){
len = m_len;
}
int i = 0;
for(i = 0;i < len;i++) {
data[i] = (*m_head);

View File

@ -21,6 +21,7 @@ include_directories("./third/jsoncpp/include/json")
include_directories("../../../obj/inc/")
include_directories("./third/gtest/include")
add_executable(ringbuffer ringbuffer_test.cpp )
add_executable(cpp11 cpp11_test.cpp )
add_executable(gtest gtest.cpp )
add_executable(thread_test thread_usage.cpp threadpool.cpp)

View File

@ -23,6 +23,7 @@ void clearBuffer(int *dat,int len){
dat[i] = 0;
}
}
void TestRingBuffer(){
int in[1024];
int out[1024];