Boost logo

Boost :

From: Itay Maman (gtpaw_at_[hidden])
Date: 2001-11-19 07:07:59


--- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> From: "Mattias Flodin" <flodin_at_c...>
> > On Sat, 17 Nov 2001, Peter Dimov wrote:
> >
> > > In order to make shared_ptr thread safe (w.r.t. reference counting) we
> need
> > > an atomic_counter type, with the following operations defined:
> > >
> > > atomic_counter a(n); // n is the initial value, convertible to 'long'
> > > ++a; // atomic increment, returns the new value (by value, type 'long')
> > > --a; // atomic decrement, returns the new value (by value, type 'long')
>
> As Carl Daniel correctly observed these need not return the new value, only
> something that is sign-compatible with it.
>

I think a better solution would be to change the implementation of
operator++() such that it will always return the correct value, which means
getting rid of the 'special' Win32 version of the class. Nonetheless, we
can still add two other member functions which will supply the
increment/decrement functionality implemented via Interlocked* calls. This
approach has its drawback - the mutex is created although it might not be
needed - but I think the positive outcome (having ++/-- return the updated
value) is more substantial.

My second suggestion is to supply an overloaded operator+=(int rhs),
operator -= (int rhs), or even operator +(int rhs) and operator- (int rhs).
Although this direction is not going well with the concept of "it's
supposed to be the minimal thing that does the job", I belive the +=/-=
functionality is pretty useful.

Itay.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk