|
Threads-Devel : |
From: joop renes (jj.renes_at_[hidden])
Date: 2008-01-31 02:37:32
On Wed, 2008-01-30 at 12:00 -0500, threads-devel-request_at_[hidden]
wrote:
> Send threads-devel mailing list submissions to
> threads-devel_at_[hidden]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.boost.org/mailman/listinfo.cgi/threads-devel
> or, via email, send a message with subject or body 'help' to
> threads-devel-request_at_[hidden]
>
> You can reach the person managing the list at
> threads-devel-owner_at_[hidden]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of threads-devel digest..."
>
>
> Today's Topics:
>
> 1. threading newbie needsadvice (joop renes)
> 2. Re: threading newbie needsadvice (Anthony Williams)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 30 Jan 2008 07:27:49 +0100
> From: joop renes <jj.renes_at_[hidden]>
> Subject: [Threads-devel] threading newbie needsadvice
> To: threads-devel_at_[hidden]
> Message-ID: <1201674469.25028.25.camel_at_van-dattum>
> Content-Type: text/plain
>
> hi all,
> in a hobby project i need to work with pointers to a family of
> polymorphic objects and i want to do multi-threaded computations with
> them. does boost have thread-safe smart pointers? i tried Loki but run
> into problems to find the correct policy classes fot the multi-threaded
> case
> any suggestions please?
> best regards
> joop renes
>
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 30 Jan 2008 09:21:48 +0000
> From: Anthony Williams <anthony_at_[hidden]>
> Subject: Re: [Threads-devel] threading newbie needsadvice
> To: threads-devel_at_[hidden]
> Message-ID: <20080130092148.sxjl7j5m1w44cwoo_at_[hidden]>
> Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes";
> format="flowed"
>
> Quoting joop renes <jj.renes_at_[hidden]>:
>
> > in a hobby project i need to work with pointers to a family of
> > polymorphic objects and i want to do multi-threaded computations with
> > them. does boost have thread-safe smart pointers? i tried Loki but run
> > into problems to find the correct policy classes fot the multi-threaded
> > case
> > any suggestions please?
>
> It depends what you're trying to do. You can safely pass shared_ptrs
> around between threads, provided you don't update the same instance
> from multiple threads at the same time, or update an instance from one
> thread whilst reading it from another. It's quite OK to copy
> shared_ptrs (even those that refer to the same object) in multiple
> threads concurrently --- the reference count is thread safe.
>
> Anthony
thanks for your speedy response.
The computatations i do can be divided into a part where shared STL
containers and the shared pointers are read only to compute an
intermediate result, followed by an update part of a shared variable.
Perhaps a piece of code tells itmore clearly:
// start of snippet
class Base;// Abstract Base Class of a hierarchy
class Key;//exports operator <
typedef boost::shared_ptr<Base> BasePtr;
typedef std::map<Key, BasePtr> BaseMap;
struct Worker : public std::unary_function<void, void>
{// signature required by threadpool
mutable long result;
mutable BaseMap::iterator bmi;
Worker( long& rl, const BaseMap::iterator& ri ) : result( rl ),
bmi( ri ){}
void operator()(){
long tmp = bmpi->second->compute();//readonly
//computation
// need some lock here
Lock lock( *this );//this is the Loki type solution
result +=tmp;
};
long computation( const BaseMap& rbm);
{
long ret;
boost::threadpool::pool tp( rbm.size()/4 );
for ( BaseMap::const_iterator i = rbm.begin(); i != rbn.end();i++)
{
tp.schedule( Worker( ret, i ) );
}
tp.wait();
return ret;
}
// end of snippet
i hope this clarifies your query
regards
joop