473,399 Members | 3,888 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,399 software developers and data experts.

fifo

How can I implement a "time critical" fifo in c++ ?
I just have to manage sequences of raw bytes, no user defined types.
An std::queue<unsigned char> or std::deque<unsigned char> seems to be slow.
I've compared that to a platform depended solution (i tried to use a MS
Window pipe in the same process to create a fifo and it's very faster).

Many thanks
Michele Moccia

--
*** MoxCdBurn ***
Add the cd writing power to your programs!
Open source C++ class and ActiveX control
www.moxcdburn.net
Mar 6 '06 #1
2 8976
Michele Moccia wrote:
How can I implement a "time critical" fifo in c++ ?
Think "circular buffer". You can probably easily wrap a vector or a deque
with two indices if you agree to have a limited size of your queue.
[...]


V
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #2
Michele Moccia wrote:
How can I implement a "time critical" fifo in c++ ?
I just have to manage sequences of raw bytes, no user defined types.
An std::queue<unsigned char> or std::deque<unsigned char> seems to be
slow. I've compared that to a platform depended solution (i tried to use a
MS Window pipe in the same process to create a fifo and it's very faster).

I don't know if this is fast (I am not even sure whether it is correct),
but if you want to try, feel free. If you get some benchmarks, please post
them:

#include <list>
#include <memory>

template < typename T, std::size_t N=1024,
typename Alloc=std::allocator<T> >
class fifo {
public:

typedef T value_type;
typedef std::size_t size_type;
typedef value_type & reference;
typedef value_type const & const_reference;
typedef value_type * pointer;
typedef value_type const * const_pointer;

private:

struct Block : public Alloc {

pointer const b;

Block ( void )
: b( Alloc::allocate(N) )
{}

void clear ( void ) {
Alloc::deallocate( b, N );
}

pointer begin ( void ) const {
return ( b );
}

pointer end ( void ) const {
return ( b+N );
}

};

std::list< Block > blocks;
size_type the_size;

pointer out_pointer;
pointer out_end;
pointer in_pointer;
pointer in_end;

public:

fifo ( void )
: blocks ( 1, Block() )
, the_size ( 0 )
, out_pointer ( blocks.front().begin() )
, out_end ( blocks.front().end() )
, in_pointer ( blocks.front().begin() )
, in_end ( blocks.front().end() )
{
assert( ! blocks.empty() );
}

bool empty ( void ) const {
return ( the_size == 0 );
}

size_type size ( void ) const {
return ( the_size );
}

reference front ( void ) {
return ( *out_pointer );
}

const_reference front ( void ) const {
return ( *out_pointer );
}

reference back ( void ) {
return ( *(in_pointer-1) );
}

const_reference back ( void ) const {
return ( *(in_pointer-1) );
}

void push ( const_reference x ) {
if ( in_pointer == in_end ) {
blocks.push_back( Block() );
in_pointer = blocks.back().begin();
in_end = blocks.back().end();
}
blocks.back().construct( in_pointer, x );
++ in_pointer;
++ the_size;
}

void pop ( void ) {
blocks.front().destroy( out_pointer );
++ out_pointer;
if ( out_pointer == out_end ) {
blocks.front().clear();
blocks.pop_front();
if ( blocks.empty() ) {
blocks.push_back( Block() );
in_pointer = blocks.back().begin();
in_end = blocks.back().end();
}
out_pointer = blocks.front().begin();
out_end = blocks.front().end();
}
-- the_size;
}

void clear ( void ) {
// FIXME: [this might be slow]
while ( the_size > 0 ) {
pop();
}
}

}; // fifo<>


Best

Kai-Uwe Bux
Mar 8 '06 #3

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

Similar topics

7
by: Tobias Pfeiffer | last post by:
Hi! I want to write a "client-server-application" (only running on the same machine) or actually I've already begun with and have problems with the interprocess communication. The server, when...
4
by: Luca | last post by:
I have the need of a container of integers showing both the characteristics of an associative container (all integer elements different from each other) and the FIFO behaviour. Do you know if...
2
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will...
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,...
5
by: Dinsdale | last post by:
I have an application that recieves text data via external input (i.e. serial) and displays it on the screen (we use carraige return as a delimiter). At this point I use a regular old text box and...
1
by: mai | last post by:
Hi everyone, i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I searched over 2000 random strings but i couldnt find any anomaly,, am i I doing it right?,, Please help,,,The...
2
by: Spoon | last post by:
Hello, I'm wondering whether the STL defines a data structure with the following features: o provides push_front() and pop_back() (like std::list) to implement a FIFO buffer. o allows fast...
5
by: doxtor | last post by:
Hi all..I'm a little bit confuse with this problem. I have 3 tables, stock_in, stock_level, and stock_out. stock_in used for save the data product when there's new purchase. stock_out used for...
40
by: sazd1 | last post by:
Hi Student2 I am working on similar kind of thing for stock calculation but could not find any solution to my problem even after putting my problem to different forums. I saw your post that you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.