Boost logo

Boost Users :

From: Kevin Heifner (heifnerk_at_[hidden])
Date: 2008-05-14 09:55:34


Selçuk Giray Özdamar wrote:
> const T& at(int index) const;

This doesn't look good.

> T& at(int index);

Or this.

> private:
> std::vector<T> m_buffer;
> boost::shared_mutex rw_mutex;
> };

> template <typename T>
> const T& EntityUpdateBuffer<T>::at( int index ) const
> {
> // acquire lock
> read_lock lock(rw_mutex);
> return m_buffer.at(index);
> }

The problem with this code is that you are returning a reference
to data that might be immediately invalidated. A push_back via
another thread can cause the referenced memory to no longer be valid.

> template <typename T>
> T& EntityUpdateBuffer<T>::at( int index )
> {
> // acquire lock
> read_lock lock(rw_mutex);
> return m_buffer.at(index);
> }

Same problem.

KevinH

-- 
Kevin Heifner  heifner @ ociweb.com  http://heifner.blogspot.com
           Object Computing, Inc. (OCI) www.ociweb.com

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