Boost logo

Boost Users :

From: dhruva (dhruvakm_at_[hidden])
Date: 2008-05-15 07:23:18


Hi,

On Wed, May 14, 2008 at 7:44 PM, Anthony Williams
<anthony_w.geo_at_[hidden]> wrote:
> Igor R. <igor_rubinov_at_[hidden]> writes:
>
> >> > 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.
> > Does push_back() really invalidate iterators to *previous* vector elements?!
>
> Yes: it might have to allocate a new chunk of memory for the vector, in which
> case it will have to move all the existing elements to the new memory, and
> free the old one.
>

The write locks and read locks in your code are identical (shared
mutex). On a call that does a push into the vector, you are modifying
data or doing a write. You should upgrade the lock you are holding
from a read to a write lock by a call to lock_upgrade (gets the thread
ability to gain exclusive lock) followed by unlock_upgrade_and_lock()
(gives up the ability and gains the exclusive lock atomically) (I may
be wrong here, requesting others to chip in), do the push and call to
unlock_upgrade().

That will be a true implementation of single write and multiple reader paradigm.

-dhruva

-- 
Contents reflect my personal views only!

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