|
-
March 11th, 2008, 12:37 PM
#1
Circular Buffer
Could someone show me how to write a circular buffer in C.
Basically my program will have multiple threads and all have to use the same buffer, some threads write to it others read, and others modify.
The syncronization is not too hard, i just cant find a circular buffer example anywhere, to see how it works.
-
March 11th, 2008, 01:25 PM
#2
Re: Circular Buffer
You could use a std: eque, push items into it and pop them from the other end. Admitted, that's a FIFO not a circular buffer.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
March 11th, 2008, 01:31 PM
#3
Re: Circular Buffer
 Originally Posted by treuss
Admitted, that's a FIFO not a circular buffer.
And also C++.
-
March 11th, 2008, 01:54 PM
#4
Re: Circular Buffer
 Originally Posted by Plasmator
And also C++. 
Well, it's a C++ forum, isn't it ?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
March 11th, 2008, 01:57 PM
#5
Re: Circular Buffer
 Originally Posted by treuss
Well, it's a C++ forum, isn't it ? 
Both C and C++, actually. It's just that the majority of the questions/responses here are related to C++.
Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.
OP: treuss' concept is still valid and of course transferable to C. I'm pretty sure that you didn't google well enough, though. =)
-
March 11th, 2008, 02:04 PM
#6
Re: Circular Buffer
Is a circular linked list good enough? That shouldn't be too hard to do.
-
March 11th, 2008, 02:05 PM
#7
Re: Circular Buffer
treuss' concept is still valid and of course transferable to C.
hmm... but a std: eque is more complicated than a circular buffer, methinks.
-
March 11th, 2008, 02:05 PM
#8
Re: Circular Buffer
A circular buffer is essentially just an array that when you get to the end of the array, you start writing at the beginning again. You'll need something to keep track of your current position, the length of the array, and something to keep track of the last read position (to prevent overwrite, if neccessary).
I'll leave the implementation up to you, as this sounds a bit like a homework problem.
-
March 11th, 2008, 03:02 PM
#9
Re: Circular Buffer
Wow so i guess when someone posts for the first time, automatically its assumed its a homework assignment lol. what a nice welcome.
Well i knew it can be implemented as an array i was asking for any examples, but whatever, ill write from scratch.
For those who want to know
the assignment is actually a conversation program that uses multiple threads for input, conversion and output. The amount of threads for each phase is specified at runtime, and only one data structure can be used, so basically tests if you can use process synchronization
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|