Boost logo

Boost :

From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-05-02 07:54:10


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_at_[hidden]>
To: <boost_at_[hidden]>
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_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of vicente.botet
> Sent: Wednesday, April 30, 2008 1:29 PM
> To: boost_at_[hidden]
> 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
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk