这是我的教授希望我们挑战的 compsci 讲座测验。他说这门课是关于循环缓冲区的,但有很大的问题。我试图思考一整天,但没有任何线索。请给我一些想法
#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;
};