473,796 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 35103
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...basical ly 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...circula r 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::CBuffe r (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

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

Similar topics

2
2800
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 and use sorting, but the problem is that i will have new data enqueued very often, and the sorting will not simulate the CIRCULAR Queue. Can any one suggest a way of doing it with query, VB, or SQL?
6
37425
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
1
2269
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 = MessageQueue.create("FormatName:DIRECT=OS:ella\private$\newq"); i encountered an error: unable to create queue. please advise.
2
8880
by: deepak | last post by:
how to create msmq queue at remote machine? n how to send messege to remote queue? -- deepak
8
1381
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 happen to hit the 'write XML' function at exactly the same time and both process vie to write the file first. As such, we need to fix that. William in this forum gave me a nice example: > Additionally, I believe you can lock (for writing by...
8
14225
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, there are ten characters in the
2
12314
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 reply if you know the solution.
2
2967
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 store up to 10 items? Output from my code: Enter you choice: 1 Enter ID document to print : 21 Enter you choice: 1 Enter ID document to print : 22
1
1494
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.
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.