CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Circular Buffer

  1. #1
    Join Date
    Mar 2008
    Posts
    2

    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.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  3. #3
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Circular Buffer

    Quote Originally Posted by treuss
    Admitted, that's a FIFO not a circular buffer.
    And also C++.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Circular Buffer

    Quote 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.

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Circular Buffer

    Quote 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. =)

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Circular Buffer

    Is a circular linked list good enough? That shouldn't be too hard to do.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Apr 2004
    Posts
    102

    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.

  9. #9
    Join Date
    Mar 2008
    Posts
    2

    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
  •  





Click Here to Expand Forum to Full Width

Featured