-1

This was from compsci lecture quiz that my professor want us to challenge. he said that this class is about circular buffer but has got major problem. I've try to think for whole day but have no clue. please give me some ideas

#include <stdint.h>
#include <boost/thread.hpp>

template <typename T, uint32_t max>
class CircularBuffer
{
public:

    CircularBuffer() : m_WriteOffset(0), m_ReadOffset(0){}
    ~CircularBuffer(){};

    void push( T val )
    {
        boost::lock_guard<boost::mutex> guard( m_Mutex );
        m_Buffer[m_WriteOffset++] = val;
    }

    T pull()
    {
        boost::lock_guard<boost::mutex> guard( m_Mutex );
        return m_Buffer[m_ReadOffset++];
    }

private:

    boost::mutex m_Mutex;

    enum { MAX_SIZE = max };
    T m_Buffer[MAX_SIZE];
    uint32_t m_WriteOffset;
    uint32_t m_ReadOffset;
};
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user842589
  • 107
  • 2
  • 12
  • To give you a hint: There is a superfluous semicolon in the code. Then, write a test with two threads, one writing N elements with a pattern, the other reading N elements and verifying the pattern. – Ulrich Eckhardt Aug 21 '13 at 05:54
  • You should include `` instead of ``. – dyp Aug 21 '13 at 05:55
  • ...and use std::thread, but that assumes you have a C++11 compiler. – Ulrich Eckhardt Aug 21 '13 at 05:56
  • If you don't know what a `mutex` and what a `lock_guard` is, then better research that first. This is basic knowledge at the level of your problem. – Walter Sep 03 '13 at 15:48

2 Answers2

1

full, empty methods not implemented. Offset increment must be like m_ReadOffset = (m_ReadOffset + 1) % MAX_SIZE, then only the buffer will be circular. Both read and write offset I mean.

Sakthi Kumar
  • 3,047
  • 15
  • 28
  • can I ask you how lock_guard works inside of the function? I know how mutex works but not with boost functions. – user842589 Aug 21 '13 at 06:13
  • sorry that I could not answer your question. I actually don't know C++, just answered the logical part of a data structure. – Sakthi Kumar Aug 21 '13 at 06:31
0

You asked for a hint, so here is one:

What happens if you push MAX_SIZE+1 elements before reading any?

Ross Bencina
  • 3,822
  • 1
  • 19
  • 33