Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr and weak_ptr concurrency
From: Stefan Strasser (strasser_at_[hidden])
Date: 2009-09-03 02:30:22


Am Thursday 03 September 2009 06:02:05 schrieb Gottlob Frege:
> >
> > <code>
> >
> > //initializion Thread (runs before any other thread):
> > shared_ptr< MyClass > global_ptr(createNewObject()) ;
> >
> > //WriterThread :
> >
> > while(true) {
> > mutex.lock()
> > global_ptr.reset( createNewObject())
> > mutex.unlock();
> > sleep (5) ;
> > }
>
> By the way, to minimize contention, call createNewObject() outside the
> lock: local = createNewObject();
> lock();
> global = local;
> unlock();
>
> > //ReaderThreads:
> >
> > mutex.lock()
> > weak_ptr<MyClass> local_weak_ptr (globally_ptr) ;
> > mutex.lock()

why would you even need a lock here?
the shared_ptr doc says that you can expect the same thread safety from
shared_ptr as you can from built-in types.
you can use multiple-readers-single-writer without any locks on built-in
types.


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