Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] thread_group::join_any
From: Gaetano Mendola (mendola_at_[hidden])
Date: 2010-06-10 16:57:13


On 01/06/2010 18.15, vicente.botet wrote:
> 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.

So what you propose is to encapsulate the original thead into a class
with the operator()() getting:

-) a reference to a condition from the thread_group (theCondition)
-) a reference to a mutex from the thread_group (theMutex)
-) a reference to the "threadfunction" being added (theOriginalFunction)

register as "threadfunction" this wrapper with the operator()()
implemented something like this:

TWrapper::operator()() {
   try {
     theOriginalFunction();
     scoped_lock myGuard(theLock);
     theCondition.notify_one();
   }
   catch(...) {
     theCondition.notify_one();
     throw;
   }
}

and then:

thread_group::join_any() {
   scoped_lock myGuard(theMutex);
   if (all threads are joinable) {
     theCondition.wait(myGuard);
     this->interrupt_all();
     this->join_all();
   } else {
     this->interrupt_all();
     this->join_all();
   }
}

I think it could work.

Regards
Gaetano Mendola


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