Boost logo

Boost :

Subject: Re: [boost] [Boost.Function] How about parallel functions?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-09-06 05:55:11


Hi,
----- Original Message -----
From: "Kasra" <kasra_n500_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, September 05, 2009 2:14 PM
Subject: [boost] [Boost.Function] How about parallel functions?

>
> Hi,
>
> I required to call few functions is separate threads and use the
> results. However, the code for this looks really ugly, you have to create
> threads, then join the threads when ever you require the result of the functions.
>
> So I have created a boost.parallel_function library that does this
> seamlessly just like invoking a normal function and it only joins the
> thread when the value of the function result type is required. Is this
> library of any interest to any one?
>
> Here is the a hello world example:
>
> #include <cstdlib>
> #include <limits>
> #include <boost/parallel_function.hpp>
> using namespace boost;
>
> int F0()
> {
> volatile int x = 0;
> for(int i=0; i<std::numeric_limits<int>::max()/1024; i++)
> x ^= std::rand();
> return (int)x;
> }
>
>
> int F2(int y)
> {
> volatile int x = 0;
> for(int i=0; i<std::numeric_limits<int>::max()/512; i++)
> x ^= std::rand();
> return (int)x ^ y;
> }
>
> int main()
> {
> typedef boost::parallel_function< void ( ) > f0_type;
> typedef boost::parallel_function< void (int) > f1_type;
>
> f0_type f0( &::F0 );
> f1_type f1( &::F1 );
>
> f0_type::result_type r0 = f0();
> f1_type::result_type r1 = f1(32);
>
> // use r0 and r1 as if they are int
> r0 += 34;
> int value = r1 *= r0 + 452;
>
> std::cout << "Hello world " << value << std::endl;
>
> return 0;
> }

The Boost.Task library should provides the features you need.

int main() {

   boost::task::handle<int> f0 = boost::task:async(boost:task(F0), boost::task::new_thread());
   boost::task::handle<int> f1 = boost::task:async(boost:task(F1,32), boost::task::new_thread());
   int r0=f0.get();
   int r1=f1.get();

   r0 += 34;
   int value = r1 *= r0 + 452;

   std::cout << "Hello world " << value << std::endl;

   return 0;
}

This library is under the review schedule and will be reviewed soon. I'm interested on the details of your implementation to compare with Boost.Task. It will be also useful if you can take a look to Boost.task and try so see it provides every thing you need.

Best,
Vicente


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