473,440 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

How to Create a Circular Queue in C++

Hi Folks

I want to create a circular queue for high speed data acquistion on
QNX with GNU C++ .
Does any one know of efficient ways either Using STL or in any other
way .
I am thing of using queue or dqueue to implement it ...
any good info is appreciated . I want it to be efficient , high speed
for developing device drivers .

Thanks
subra

Jan 10 '06 #1
10 35027
What type of data will you be storing in the queue? For the least
amount of overhead, why not make an array of said type, and a counting
number that is incremented until a certain number, then set to 0
again...basically a circular buffer in an array. Unless you need a
start and end counter...make sure that they don't cross.

Also, there is a queue class: http://www.cppreference.com/cppqueue/

Now that I think of it, I don't know what you mean by circular
queue...circular buffer? I'm losing it.

Jan 10 '06 #2
On 10 Jan 2006 10:35:37 -0800, av***@mailcity.com wrote:
I want to create a circular queue for high speed data acquistion on
QNX with GNU C++ . Does any one know of efficient ways either Using
STL or in any other way .
I am thing of using queue or dqueue to implement it ...
any good info is appreciated . I want it to be efficient , high speed
for developing device drivers .


Enter 'circular queue' here: http://www.koders.com/

Best wishes,
Roland Pibinger
Jan 10 '06 #3
Do not use STL queue's unless you are sure you can predict all
allocations/deallocations precisely. It is best you make your own
circular queue that has guaranteed allocator (non) usage, or find one
specifically made for device drivers. Additionally some (easily
avoided) STL container functions throw exceptions, which should never
be used in a device-driver (unless you have a magic compiler which
creates easily predictable throw/catch behavior).

Again, the key point is to make sure you have COMPLETE control over any
unbounded operations (most often allocations/deallocations), if you can
find a way to use an STL container in such a way, go for it.

Jeremy Jurksztowicz

Jan 10 '06 #4
TB
av***@mailcity.com sade:
Hi Folks

I want to create a circular queue for high speed data acquistion on
QNX with GNU C++ .
Does any one know of efficient ways either Using STL or in any other
way .
I am thing of using queue or dqueue to implement it ...
any good info is appreciated . I want it to be efficient , high speed
for developing device drivers .

Thanks
subra


Depends upon design, and how fluffy you want your component to be,
in either case, here's a fixed sized circular queue/buffer:

int CQ[1024];

void write(int t) {
static unsigned int idx = 0;
CQ[idx++%1024] = t;
}

int read() {
static unsigned int idx = 0;
return CQ[idx++%1024];
}

TB

Jan 10 '06 #5
Hi

This implementation and the one suggested by Mr Benry above have a
flaw .
If the writing and reading are happening at different speeds which
would be the case
and which is the whole idea behind the circular queues (to discard the
old data and to get the old data first and then the new data of the
sna shot we have in the queue ).
After the buffer is full and the write starts writing in the beginning
, but the reader
in the situation where it gets to the beginning and start reading from
there could find the
latest data there and after some time when it gets to the next index it
could find old data
there , i.e it could get latest data at some time and a little later
could get old data which should not be the case .
please clarify if iam wrong

Thanks
subra

Depends upon design, and how fluffy you want your component to be,
in either case, here's a fixed sized circular queue/buffer:

int CQ[1024];

void write(int t) {
static unsigned int idx = 0;
CQ[idx++%1024] = t;
}

int read() {
static unsigned int idx = 0;
return CQ[idx++%1024];
}

TB


Jan 10 '06 #6
av***@mailcity.com wrote:
Hi

This implementation and the one suggested by Mr Benry above have a
flaw .
If the writing and reading are happening at different speeds which
would be the case
and which is the whole idea behind the circular queues (to discard the
old data and to get the old data first and then the new data of the
sna shot we have in the queue ).
After the buffer is full and the write starts writing in the beginning
, but the reader
in the situation where it gets to the beginning and start reading from
there could find the
latest data there and after some time when it gets to the next index it
could find old data
there , i.e it could get latest data at some time and a little later
could get old data which should not be the case .
please clarify if iam wrong

Please don't top-post (i.e., you reply goes after what you're replying
to). You are correct that the code below may read data out of order.
To get around this you need to keep track of more information. Here's
one approach off the top of my head-- it may not be optimal.

Keep a couple boolean state variables:
bool bufferEmpty; // initially true
bool bufferFull; // initially false

bufferEmpty = true means there's nothing available to read.
bufferFull = true means the entire contents of the buffer have been
written to but not been read.

Now you have to devise rules for reading and writing that update these
quantities appropriately:

If bufferEmpty:
read = error
write = advance write index, unset bufferEmpty

If bufferFull:
read = advance read index; unset bufferFull
write = advance read and write indices

If !bufferEmpty && !bufferFull:
read = advance read index; if read index = write index, set bufferEmpty
write = advance write index; if read index = write index, set bufferFull

Like I said, this is improvised so you should check it and correct it if
needed. Hope that helps.

-Mark
Thanks
subra

Depends upon design, and how fluffy you want your component to be,
in either case, here's a fixed sized circular queue/buffer:

int CQ[1024];

void write(int t) {
static unsigned int idx = 0;
CQ[idx++%1024] = t;
}

int read() {
static unsigned int idx = 0;
return CQ[idx++%1024];
}

TB


Jan 10 '06 #7
Mark P wrote:
av***@mailcity.com wrote:
Hi

This implementation and the one suggested by Mr Benry above have a
flaw .
If the writing and reading are happening at different speeds which
would be the case
and which is the whole idea behind the circular queues (to discard the
old data and to get the old data first and then the new data of the
sna shot we have in the queue ).
After the buffer is full and the write starts writing in the beginning
, but the reader
in the situation where it gets to the beginning and start reading from
there could find the
latest data there and after some time when it gets to the next index it
could find old data
there , i.e it could get latest data at some time and a little later
could get old data which should not be the case .
please clarify if iam wrong


Please don't top-post (i.e., you reply goes after what you're replying
to). You are correct that the code below may read data out of order. To
get around this you need to keep track of more information. Here's one
approach off the top of my head-- it may not be optimal.

Keep a couple boolean state variables:
bool bufferEmpty; // initially true
bool bufferFull; // initially false

bufferEmpty = true means there's nothing available to read.
bufferFull = true means the entire contents of the buffer have been
written to but not been read.

Now you have to devise rules for reading and writing that update these
quantities appropriately:

If bufferEmpty:
read = error
write = advance write index, unset bufferEmpty

If bufferFull:
read = advance read index; unset bufferFull
write = advance read and write indices

If !bufferEmpty && !bufferFull:
read = advance read index; if read index = write index, set bufferEmpty
write = advance write index; if read index = write index, set bufferFull

Like I said, this is improvised so you should check it and correct it if
needed. Hope that helps.

-Mark


Just for fun, here's some code which seems to do the right thing. Use
at your own risk...

-Mark

#include <vector>
#include <iostream>

class CBuffer
{
public:
CBuffer (unsigned int size);
int read ();
void write (int value);

// simple test functions
void testWrite ()
{
std::cout << "write " << ++count << std::endl;
write(count);
}

void testRead ()
{
std::cout << "read " << read() << std::endl;
}

private:
std::vector<int> buffer;
unsigned int size;
unsigned int rIdx;
unsigned int wIdx;
bool empty;
bool full;

int count; // index for test functions
};

CBuffer::CBuffer (unsigned int size) :
buffer(size), size(size), rIdx(0), wIdx(0),
empty(true), full(false), count(0) {}

int CBuffer::read()
{
if (empty)
{
std::cerr << "Error: can't read from empty buffer." << std::endl;
exit(1);
}
full = false; // buffer can't be full after a read
int result = buffer[rIdx];
rIdx = (rIdx + 1) % size;
empty = (rIdx == wIdx); // true if read catches up to write
return result;
}

void CBuffer::write (int value)
{
empty = false; // buffer can't be empty after a write
buffer[wIdx] = value;
wIdx = (wIdx + 1) % size;
if (full)
rIdx = wIdx;
full = (wIdx == rIdx); // true if write catches up to read
}

int main()
{
CBuffer cb(4);
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testRead();
cb.testRead();

cb.testWrite();
cb.testWrite();
cb.testRead();
cb.testRead();
cb.testRead();

cb.testWrite();
cb.testWrite();
cb.testRead();
cb.testRead();

cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testWrite();
cb.testRead();
cb.testRead();
}
Jan 11 '06 #8
Make it a template class and put some policies for dealing with
overflow and underflow (maybe with some throws) and you will have a
good utility class for general purpose circular queues

Jan 11 '06 #9
Well, I explained that with a pointer to the location:

beginning end
\/---------------------------------------------------\/

this would be how it starts. Now, when data starts to be written to
the queue:
beginning end
--------------\/-------------------------------------\/

And when you start reading:

end beginning
---\/-----------\/--------------------------------------

However, the situation you're talking about is if beginning "laps" end,
and starts to write new data over data that is still in the queue. I
wrote something where beginning and end can never cross.

Jan 12 '06 #10
Thank you folks ,

Thanks for all the info , i was looking for ring buffer which can be
used across processes .
Benry yes you did indicate it the data crossing over with pointers i am
sorry i overlookd it .

thanks again
subra

Jan 12 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zhaozhi Gao | last post by:
I'm doing a simple project for school. This is the first time I've ever used Access. Is there a way i can simulate a Circular Queue data structure for the datas in a table. I tried assign index...
6
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
1
by: baby ell | last post by:
I'm accessing a private queue in another machine and if the queue does not exists in that machine, i would like to create the private queue. Example: MessageQueue newmsg =...
2
by: deepak | last post by:
how to create msmq queue at remote machine? n how to send messege to remote queue? -- deepak
8
by: darrel | last post by:
We're having an odd problem with our home grown CMS once in a great while, an XML file will become corrupt...either missing, or only half-written. We think something is happening when two people...
8
by: Jack | last post by:
I want to implement a fixed-size FIFO queue for characters. I only want to use array not linked list. For example, const int N = 10; char c_array; The question is when the queue is full,...
2
by: soniya_batra1982 | last post by:
I have a problem in imploementation of circular queue through array in C language. Please send me the code of functions for insertion, deletion and display the elements of array. Plz send me quick...
2
by: lavender | last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10. Where is the wrong in my code? Why it cannot...
1
by: manziba | last post by:
i have to insert and delete in queue.using malloc() function.And two fies delete.c and insert.c two source file is to be created.And q.h which is header file is to be created.Help me.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.