[thread]joining the first thread finishing from a group of threads

Hello, I have several implementations of the same function with different algorithms. I want to launch these in parallel and join the one finishing the first, and stop the others. I have started and in my solution I need to wrap the function in order to make the needed synchronization, which seam not quite satisfactory. Is there an easy way to join the first thread that finish from a thread group and getting it? boost::thread_group tg; // ... boost::thread* first = tg.join_one(); Do you find useful this feature? Regards _____________________ Vicente Juan Botet Escriba

I think boost::barrier is what you look for. Supposed an algorithm function looks like this void MyAlgorithm( boost::barrier& i_FistAlgorithmDone ) { // run algo //... i_FistAlgorithmDone.wait(); } Then the main thread would look like this: // 2 waits, one from the main thread, the other from the first algo boost::barrier FistAlgorithmDone( 2 ); boost::thread_group tg; // ... // pass in FistAlgorithmDone to your algorithm functions Tg.create_thread( std::tr1::bind( &MyAlgorithm, std::tr1::ref( FistAlgorithmDone ) ); // crate next thread ... // wait for the first algo to finish FistAlgorithmDone.wait(); // wait for the remaining algos to finish boost::thread* first = tg.join_one(); I can't verify if this compiles atm. Sry for that. I hope this is what you look for. Best Jochen -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of vicente.botet Sent: Wednesday, April 30, 2008 1:29 PM To: boost@lists.boost.org Subject: [boost] [thread]joining the first thread finishing from a group ofthreads Hello, I have several implementations of the same function with different algorithms. I want to launch these in parallel and join the one finishing the first, and stop the others. I have started and in my solution I need to wrap the function in order to make the needed synchronization, which seam not quite satisfactory. Is there an easy way to join the first thread that finish from a thread group and getting it? boost::thread_group tg; // ... boost::thread* first = tg.join_one(); Do you find useful this feature? Regards _____________________ Vicente Juan Botet Escriba _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hi Jochen, There is a problem with the barrier approach. if we have two threads, the second thread will wait on the barrier until some one else call to wait. This give me an idea. What about a semaphore? void MyAlgorithm( semaphore& done ) { // run algo //... done.post(); } semaphore FistAlgorithmDone( 0 ); // ... // wait for the first algo to finish FistAlgorithmDone.wait(); // wait for the remaining algos to finish boost::thread* first = tg.join_all(); Boost.Thread do not have any more a semaphore class, but Boost.Interprocess has one. As both algoritms must follow the same structure we can use a function wrapper that do this void signal_when_end( semaphore& done, const boost::function<void()>& fct) { fct() done.post(); } tg.create_thread( std::tr1::bind( &signal_when_end, std::tr1::ref(FistAlgorithmDone), MyAlgorithm1); tg.create_thread( std::tr1::bind( &signal_when_end, std::tr1::ref(FistAlgorithmDone), MyAlgorithm2); What I would really want to be able to write is thread_all(MyAlgorithm1, MyAlgorithm2).join_one(); I'll continue to work on. Thanks _____________________ Vicente Juan Botet Escriba ----- Original Message ----- From: "Heckl, Jochen (ext)" <jochen.heckl.ext@siemens.com> To: <boost@lists.boost.org> Sent: Wednesday, April 30, 2008 4:21 PM Subject: Re: [boost] [thread]joining the first thread finishing from a groupofthreads
I think boost::barrier is what you look for.
Supposed an algorithm function looks like this
void MyAlgorithm( boost::barrier& i_FistAlgorithmDone ) { // run algo //...
i_FistAlgorithmDone.wait(); }
Then the main thread would look like this:
// 2 waits, one from the main thread, the other from the first algo boost::barrier FistAlgorithmDone( 2 );
boost::thread_group tg; // ...
// pass in FistAlgorithmDone to your algorithm functions Tg.create_thread( std::tr1::bind( &MyAlgorithm, std::tr1::ref( FistAlgorithmDone ) ); // crate next thread ...
// wait for the first algo to finish FistAlgorithmDone.wait();
// wait for the remaining algos to finish boost::thread* first = tg.join_one();
I can't verify if this compiles atm. Sry for that. I hope this is what you look for.
Best Jochen
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of vicente.botet Sent: Wednesday, April 30, 2008 1:29 PM To: boost@lists.boost.org Subject: [boost] [thread]joining the first thread finishing from a group ofthreads
Hello,
I have several implementations of the same function with different algorithms. I want to launch these in parallel and join the one finishing the first, and stop the others.
I have started and in my solution I need to wrap the function in order to make the needed synchronization, which seam not quite satisfactory.
Is there an easy way to join the first thread that finish from a thread group and getting it?
boost::thread_group tg; // ... boost::thread* first = tg.join_one();
Do you find useful this feature?
Regards _____________________ Vicente Juan Botet Escriba _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hi Vincete, I already noticed that the barrier solution would not work, and posted, but I think the post was lost somewhere on the way while switching around in my profile... sry for that. As an alternative I suggested boost::condition which I think pretty much matches your solution. Nevertheless sry for leading you the wrong way in the first place.
What I would really want to be able to write is
thread_all(MyAlgorithm1, MyAlgorithm2).join_one();
what about this? does this do? thread_all( std::tr1::function< void ( void ) > i_Call01, std::tr1::function< void ( void ) > i_Call02 ) : { boost::mutex M; boost::unique_lock<boost::mutex> Lock(M); boost::condition_variable Cond; ... // start i_Call01 and Call02 wrapped in signal_when_end // with void signal_when_end( boost::condition_variable& i_Cond, const boost::function<void()>& fct) // { // fct(); // _Cond.notify_one(); // } ... Cond.wait(Lock); } Best Jochen On Fri, May 2, 2008 at 1:54 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
Hi Jochen,
There is a problem with the barrier approach. if we have two threads, the second thread will wait on the barrier until some one else call to wait. This give me an idea. What about a semaphore?
void MyAlgorithm( semaphore& done ) { // run algo //...
done.post(); }
semaphore FistAlgorithmDone( 0 ); // ...
// wait for the first algo to finish FistAlgorithmDone.wait();
// wait for the remaining algos to finish boost::thread* first = tg.join_all();
Boost.Thread do not have any more a semaphore class, but Boost.Interprocess has one.
As both algoritms must follow the same structure we can use a function wrapper that do this
void signal_when_end( semaphore& done, const boost::function<void()>& fct) { fct() done.post(); }
tg.create_thread( std::tr1::bind( &signal_when_end, std::tr1::ref(FistAlgorithmDone), MyAlgorithm1); tg.create_thread( std::tr1::bind( &signal_when_end, std::tr1::ref(FistAlgorithmDone), MyAlgorithm2);
What I would really want to be able to write is
thread_all(MyAlgorithm1, MyAlgorithm2).join_one();
I'll continue to work on.
Thanks _____________________ Vicente Juan Botet Escriba ----- Original Message ----- From: "Heckl, Jochen (ext)" <jochen.heckl.ext@siemens.com> To: <boost@lists.boost.org> Sent: Wednesday, April 30, 2008 4:21 PM Subject: Re: [boost] [thread]joining the first thread finishing from a groupofthreads
I think boost::barrier is what you look for.
Supposed an algorithm function looks like this
void MyAlgorithm( boost::barrier& i_FistAlgorithmDone ) { // run algo //...
i_FistAlgorithmDone.wait(); }
Then the main thread would look like this:
// 2 waits, one from the main thread, the other from the first algo boost::barrier FistAlgorithmDone( 2 );
boost::thread_group tg; // ...
// pass in FistAlgorithmDone to your algorithm functions Tg.create_thread( std::tr1::bind( &MyAlgorithm, std::tr1::ref( FistAlgorithmDone ) ); // crate next thread ...
// wait for the first algo to finish FistAlgorithmDone.wait();
// wait for the remaining algos to finish boost::thread* first = tg.join_one();
I can't verify if this compiles atm. Sry for that. I hope this is what you look for.
Best Jochen
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of vicente.botet Sent: Wednesday, April 30, 2008 1:29 PM To: boost@lists.boost.org Subject: [boost] [thread]joining the first thread finishing from a group ofthreads
Hello,
I have several implementations of the same function with different algorithms. I want to launch these in parallel and join the one finishing the first, and stop the others.
I have started and in my solution I need to wrap the function in order to make the needed synchronization, which seam not quite satisfactory.
Is there an easy way to join the first thread that finish from a thread group and getting it?
boost::thread_group tg; // ... boost::thread* first = tg.join_one();
Do you find useful this feature?
Regards _____________________ Vicente Juan Botet Escriba _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (3)
-
Heckl, Jochen (ext)
-
Jochen Heckl
-
vicente.botet