[buffer] fix read index

This commit is contained in:
Jan Grewe 2020-03-13 17:40:22 +01:00
parent d336000553
commit f68dc517ae
2 changed files with 3 additions and 4 deletions

View File

@ -33,7 +33,7 @@ void ImageBuffer::resize(size_t new_size) {
buffer_size = new_size; buffer_size = new_size;
buffer.clear(); buffer.clear();
buffer.resize(new_size); buffer.resize(new_size);
current_read_index = -1; current_read_index = 0;
current_write_index = -1; current_write_index = -1;
load = 0; load = 0;
mutex.unlock(); mutex.unlock();
@ -46,7 +46,6 @@ void ImageBuffer::push(const MyImage &img) {
if (load < static_cast<int64_t>(buffer.capacity())) if (load < static_cast<int64_t>(buffer.capacity()))
load += 1; load += 1;
mutex.unlock(); mutex.unlock();
//std::cerr << "load: " << bufferLoad() << "\t pressure: " << bufferPreassure() << std::endl;
} }
bool ImageBuffer::bufferNotEmpty() { bool ImageBuffer::bufferNotEmpty() {
@ -61,9 +60,11 @@ bool ImageBuffer::pop(MyImage &img) {
bool ret = false; bool ret = false;
mutex.lock(); mutex.lock();
if (load > 0) { if (load > 0) {
std::cerr << "Buffer.pop, load: " << load << " read_idx: " << current_read_index << " write_idx: " << current_write_index << std::endl;
img = buffer[static_cast<size_t>(current_read_index)]; img = buffer[static_cast<size_t>(current_read_index)];
static_cast<size_t>(current_read_index) < (buffer.capacity() - 1) ? current_read_index += 1 : current_read_index = 0; static_cast<size_t>(current_read_index) < (buffer.capacity() - 1) ? current_read_index += 1 : current_read_index = 0;
load -= 1; load -= 1;
ret = true;
} }
mutex.unlock(); mutex.unlock();
return ret; return ret;

View File

@ -24,8 +24,6 @@ public:
bool bufferNotEmpty(); bool bufferNotEmpty();
private: private:
// QWaitCondition bufferNotEmpty; // signals that the consumer can get data
// QWaitCondition bufferNotFull; // signals that the producer can store data
QMutex mutex; QMutex mutex;
int numUsedBytes = 0; int numUsedBytes = 0;