Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] thread_group::join_any
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-06-01 12:15:11


Hi,
----- Original Message -----
From: "Gaetano Mendola" <mendola_at_[hidden]>
To: <threads-devel_at_[hidden]>
Sent: Monday, May 31, 2010 5:05 PM
Subject: [Threads-devel] thread_group::join_any

>
> Hi all,
> in my application I found myself to use the following pattern:
>
> 1) create a group of thread
> 2) add some threads to this group
> 3) wait until one of the threads in this group completed then
> interrupt/join all.
>
> To do this I wrote my own class ThreadGroup with the method
> join_any doing exactly the operation at the point 3.
>
> May be would be usefull, for other thread_group users have such
> functionality as well.

Sure. I think this is useful.
 
> The method should sound like this (I didn't try it):
>
> void join_any()
> {
> boost::shared_lock<shared_mutex> guard(m);
> bool one_join_success = false;
>
> while(!one_join_success) {
> for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
> it!=end;
> ++it)
> {
> one_join_success = (*it)->timed_join(boost::posix_time::millisec(10));
> if (one_join_success) {
> interrupt_all();
> break;
> }
> }
> }
> join_all();
> }

The problem with your implementation is that use polling to joing a thread.

I think that join_any could be implemented by adding a condition that will be notified by each one of the threads. Even if this could seam intrusive we can define thread_group class that creates the threads and wraps the call to the user function with the condition.
 
> if you think is worth to have it I can provide a patch.

Boost.Interthreads provides already something like that https://svn.boost.org/trac/boost/wiki/LibrariesUnderConstruction#Boost.InterThreads. I know that this needs to be polished.

I'm interested in knowing what do you think of this approach.

Best,
Vicente


Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk