|
Boost : |
From: Boris Fomitchev (fbp_at_[hidden])
Date: 1999-12-28 15:45:57
I do believe good strategy is to add default bool template argument :
template < class T, bool Synchronized = false>
class shared_ptr
.....
shared_ptr then should or should not use atomic operations depending
on Synchronized value. On most platforms, difference would not be
noticeable, though (using assembly), so it might even make sense to
default to Synchronized=true or even use it always.
SGI STL provide examples on how it can be done.
-Boris.
Greg Colvin wrote:
> From: David Abrahams (root) <abrahams_at_[hidden]>
> > scleary_at_[hidden] wrote:
> > >
> > > Giora Unger wrote:
> > >
> > > > 1. As far as I understand, the library is NOT thread safe.
> > > > Am I wrong ?
> > > > For example, let's examine line 169 in smart_ptr.h:
> > > > if (--*pn == 0) { delete px; }
> > > > I believe, that in case of two threads concurrently accessing
> > > > this line (the reset() method), and an initial reference count==2,
> > > > then a crash is possible: if both decrements occur before both
> > > > comparisons, resulting in two deletion attempts.
> > >
> > > The smart pointers are just as thread-safe as integers.
> >
> > I don't think that's entirely correct. On many platforms, all integer
> > operations are atomic; the same can't be said of our smart pointers.
>
> And on many platforms they are not, so neither are safe.
>
> > > To use them in a
> > > thread-safe way, protect them just as you would any other non-thread-safe
> > > fundamental type/class/library.
> >
> > Good advice.
>
> But potentially too slow, as critical sections are more than
> you need.
>
> > > Should we look at doing a portable multi-threading library? I have the
> > > beginnings of one, but it doesn't allow timed waits or waiting for multiple
> > > objects -- would these be considered essential or optional?
> >
> > Oh, optional, optional (though highly desired)!
> > Portable multi-threading is badly needed!
>
> For shared_ptr all you need is atomic increment and atomic
> decrement-and-test. For Win32 you can do these in assembly
> or use the InterlockedIncrement and InterlockedDecrement
> functions.
>
> However, it may not be necessary to protect every operation
> on shared_ptr. It may be that your multithreaded program
> already establishes critical sections within which you can
> do all your shared_ptr operations.
>
> ------------------------------------------------------------------------
> GET $100 IN COUPONS FOR TRYING GATOR!
> Grab the Gator! Free software does all the typing for you!
> Gator fills in forms and remembers passwords with NO TYPING at
> over 100,000 web sites!
> http://click.egroups.com/1/341/1/_/9351/_/946407682
>
> -- 20 megs of disk space in your group's Document Vault
> -- http://www.egroups.com/docvault/boost/?m=1
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk