|
Boost Users : |
From: Selçuk Giray Özdamar (selcukgiray_at_[hidden])
Date: 2008-05-14 09:42:28
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---
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net