Picon

shared container class with read/write mutex

Hi everyone, can anyone send a simple container class implementation, which supports concurrent reads and exclusive write. I’m seeking for the usage of shared_lock for read/write mutex. My simple template class snippet is shown below, please reply how to fill the comments for locking. Thnx.

 

 

// std includes

#include <vector>

 

// boost includes

#include <boost/thread/shared_mutex.hpp>

 

template <typename T>

class SharedContainer

{

public:

       SharedContainer (void){}

       ~SharedContainer (void){}

      

       void push_back(const T& value);      

       const T& at(int index) const;

 

private:

       std::vector<T> m_buffer;

       boost::shared_mutex rw_mutex; //is this the correct class for rw mutex?

};

 

template <typename T>

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

{

       // how to acquire a write lock?

 

       m_buffer.push_back(value);

}

 

template <typename T>

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

{

       // how to acquire read lock?

 

       return m_buffer.at(index);

}

 

 

Selcuk Giray

 

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Igor R. | 21 May 09:51
Picon
Favicon

Re: shared container class with read/write mutex

Hi,
 
Please, read the shared_mutex reference:
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_types.shared_mutex
as well as lock reference:
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html#thread.synchronization.locks
 
Ususally you lock the mutex like this:
{ // the scope you wish to protect
  scoped_lock(mutextObject); // - to make exclusive lock or shared_lock(mutextObject) to make shared lock
  /// do your stuff here... 
}

However, it was already discussed here that such a container will NOT be safe, because adding an element to std::vector might cause moving of all its elements, so the reference previously returned by at() won't be valid.
However, if you really do not need to remove elements from this container, then using std::deque instead of std::vector would solve this problem.

.ExternalClass p.EC_MsoNormal, .ExternalClass li.EC_MsoNormal, .ExternalClass div.EC_MsoNormal {margin-bottom:.0001pt;font-size:11.0pt;font-family:'Calibri','sans-serif';} .ExternalClass a:link, .ExternalClass span.EC_MsoHyperlink {color:blue;text-decoration:underline;} .ExternalClass a:visited, .ExternalClass span.EC_MsoHyperlinkFollowed {color:purple;text-decoration:underline;} .ExternalClass span.EC_EmailStyle17 {font-family:'Calibri','sans-serif';color:windowtext;} .ExternalClass .EC_MsoChpDefault {;} <at> page Section1 {size:612.0pt 792.0pt;} .ExternalClass div.EC_Section1 {page:Section1;}

> template <typename T>

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

> {

>       // how to acquire a write lock?

>       m_buffer.push_back(value);

> }

> template <typename T>

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

> {

>       // how to acquire read lock?

>  

>       return m_buffer.at(index);

> }

 

 


Explore the seven wonders of the world Learn more!
_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Gmane