Hi everyone,
Thanks for the prev. read/write mutex replies. I wrote a simple container which support concurrent read support and exclusive write support. Is this code correct, I havent write test code for it yet.
Tnx...
 

#pragma once

#ifndef ENTITYUPDATEBUFFER_H___

#define ENTITYUPDATEBUFFER_H___

 

// std includes

#include <vector>

 

// boost includes

#include <boost/thread/shared_mutex.hpp>

 

template <typename T>

class EntityUpdateBuffer

{

public:

       typedef typename boost::shared_lock<boost::shared_mutex> read_lock;

       typedef typename boost::unique_lock<boost::shared_mutex> write_lock;

 

       EntityUpdateBuffer(void){}

       ~EntityUpdateBuffer(void){}

      

       void push_back(const T& value);

      

       const T& at(int index) const;

      

       T& at(int index);

 

private:

       std::vector<T> m_buffer;

       boost::shared_mutex rw_mutex;

};

 

template <typename T>

void EntityUpdateBuffer<T>::push_back( const T& value )

{

       // acquire lock

       write_lock lock(rw_mutex);

 

       m_buffer.push_back(value);

}

 

template <typename T>

const T& EntityUpdateBuffer<T>::at( int index ) const

{

       // acquire lock

       read_lock lock(rw_mutex);

 

       return m_buffer.at(index);

}

 

template <typename T>

T& EntityUpdateBuffer<T>::at( int index )

{

       // acquire lock

       read_lock lock(rw_mutex);

 

       return m_buffer.at(index);

}

 

#endif //ENTITYUPDATEBUFFER_H___


 
---Sgo---