Boost logo

Boost :

Subject: [boost] [Boost.Function] How about parallel functions?
From: Kasra (kasra_n500_at_[hidden])
Date: 2009-09-05 08:14:44


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;
}

With Best Regards

Mr. Kasra Nassiri
Department of Mathematics &
Computer Science
Imperial College London

      


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